User Authentication Basics
Overview
Skygear supports user authentication with email or username. This guide will show you how it works using the Skygear Android SDK.
The global Container
named skygear
will be used throughout the examples in this guide:
Basic concept
Access token
Skygear handles the user login session using an access token stored in the local storage.
Each user, when logged in, will be given a generated String called Access Token
, which is like an identification card for the user. It is used by the Skygear server to identify who you are.
The method whoami
illustrates the usage of Access Token
. When it is called, Skygear will send the current Access Token
to the server and the server will return a User
object.
Database operation
The authenticated states of a user will affect the database operation he can perform.
Records in the public database can be queried without any user session. But if you want to write and/or update any records, a user session is required.
Signing up
Signing up with email or username
A user can sign up using a username or an email, along with a password.
It is done using either signupWithUsername
or
signupWithEmail
.
Skygear does not allow duplicated usernames or emails. Signing up with a
duplicated identifier will give the error Duplicated
.
While each of the sign-up functions is resolved with a user record,
in most cases you need not deal with it because
you can access the currently logged-in user using getCurrentUser
.
signupWithUsername
sample code:
signupWithEmail
sample code:
It is common to add other data to user record when signing up, you can do that by specifying the data for user record in the third parameter of the signup functions:
Signing up anonymously
Without being authenticated, a user can read data from the public database but cannot perform most of the other operations, including saving data into the database.
If you need an authenticated user but do not require a user to
sign up explicitly with a username or email, you can create an anonymous user
by calling signupAnonymously
.
Every anonymous user has a unique user ID, and behaves exactly the same as any user authenticated with a username or an email. The only difference is that an anonymous user has no username, email, nor password. Because of the absence of username and email, the account will be lost when the access token is lost.
Logging in
The login functions are similar to the sign-up ones.
If the credentials are incorrect, it will give the error of:
InvalidCredentials
if the password is incorrect;ResourceNotFound
if the email or username is not found.
While each of the login functions is resolved with a user record,
in most cases you need not deal with it because
you can access the currently logged-in user using getCurrentUser
.
Logging in using a username
Logging in using an email
Logging out
Logging out the current user is simple using the logout
method.
Upon successful logout, the SDK will clear the current user and the access token from the local storage.
Getting the current User
You can retrieve the current user from getCurrentUser
. Please make sure the
current user has already signed up and had a username specified.
If there is an authenticated user, it will give you a Record
which
is the user record. The user record looks like this:
{
'_id': 'abcdef',
'username': 'Ben',
'email': 'ben@skygeario.com',
}
Please be reminded that the getCurrentUser
object persist locally, and the data in the user record
might not sync with the server if it was changed remotely.
To get the latest information of the current user,
you can call whoami
:
Updating a user's username and email
Username and email is saved to the user record. You can modify the username
and email in the same way you modify any other record, which is achieved by
calling save
.
Caution: When saving user record, the user record returned by
getCurrentUser
is not automatically updated. To force an update of
the record returned from getCurrentUser
, call whoami
.
To change the username of the current user:
To change the email of the current user:
You can even change the username and email at the same time:
Updating a user's password
The currently logged-in user can change his/her own password.
This can be done using the changePassword
method.
If the current password is incorrect, the SDK will return an
INVALID_CREDENTIALS
error.
Forgot password
Coming soon.
What's next from here?
You may want to learn more about: