User Authentication Basics
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:password:completionHandler:
or
signupWithEmail:password:completionHandler:
.
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 currentUser
.
signupWithUsername:password:completionHandler:
sample code:
signupWithEmail:password:completionHandler:
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:
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 currentUser
.
Logging in using a username
Logging in using an email
Logging out
Logging out the current user is simple using the logoutWithCompletionHandler:
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 currentUser
.
If there is an authenticated user, it will give you a SKYRecord
which
is the user record. The user record looks like this:
{
'_id': 'abcdef',
'username': 'Ben',
'email': 'ben@skygeario.com',
}
Please be reminded that the currentUser
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 getWhoAmIWithCompletionHandler:
:
Observing user changes
The preferred way for your app to handle any logged-in user change is to
add observer for the SKYContainerDidChangeCurrentUserNotification
notification.
The notification is pushed whenever the user is changed, i.e.
when any of the followings happens:
- a user logs in;
- a user logs out (or as logged out due to an expired access token);
The callback will receive the new user record as the argument.
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 saveRecord:completion:
.
Caution: When saving user record, the user record returned by
currentUser
is not automatically updated. To force an update of
the record returned from currentUser
, call getWhoAmIWithCompletionHandler:
.
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 setNewPassword:oldPassword:completionHandler:
function.
If the current password is incorrect, the SDK will return an
InvalidCredentials
error.
Note: Changing the password of the current user will not cause
SKYContainerDidChangeCurrentUserNotification
notification to be posted.
Forgot password
Coming soon.
User Verification
Not yet implemented.
What's next from here?
You may want to learn more about: