Navigation

Manage Email/Password Users

Overview

When you have enabled the email/password provider in your Realm app, you can register a new account, confirm an email address, and reset a user’s password from client code.

Register a New User Account

To register a new user, pass a user-provided email and password to the registerUser() or registerUserAsync() methods of your Realm App’s EmailPasswordAuth instance:

app.emailPasswordAuth.registerUserAsync(email, password) {
   if (it.isSuccess) {
       Log.i(TAG,"Successfully registered user.")
   } else {
       Log.e(TAG,"Failed to register user: ${it.error}")
   }
}
app.getEmailPasswordAuth().registerUserAsync(email, password, it -> {
   if (it.isSuccess()) {
       Log.i(TAG,"Successfully registered user.");
   } else {
       Log.e(TAG,"Failed to register user: ${it.error}");
   }
});

Confirm a New User’s Email Address

To confirm a newly-created user, pass a confirmation token and tokenId to the confirmUser() or confirmUserAsync methods of your Realm App’s EmailPasswordAuth instance:

// token and tokenId are query parameters in the confirmation
// link sent in the confirmation email.
app.emailPasswordAuth.confirmUserAsync(token, tokenId) {
   if (it.isSuccess) {
       Log.i(TAG, "Successfully confirmed new user.")
   } else {
       Log.e(TAG, "Failed to register user: ${it.error}")
   }
}
// token and tokenId are query parameters in the confirmation
// link sent in the confirmation email.
app.getEmailPasswordAuth().confirmUserAsync(token, tokenId, it -> {
   if (it.isSuccess()) {
       Log.i(TAG, "Successfully confirmed new user.");
   } else {
       Log.e(TAG, "Failed to register user: ${it.error}");
   }
});

Note

To access the token and tokenId values sent in the user confirmation email, you can use a custom confirmation email subject containing a deep link.

Reset a User’s Password

To reset a user’s password, first send the user a password reset email:

app.emailPasswordAuth.sendResetPasswordEmailAsync(emailAddress) {
   if (it.isSuccess) {
       Log.i(TAG, "Successfully sent the user a reset password link to $emailAddress")
   } else {
       Log.e(TAG, "Failed to send the user a reset password link to $emailAddress")
   }
}
app.getEmailPasswordAuth().sendResetPasswordEmailAsync(emailAddress, it -> {
   if (it.isSuccess()) {
       Log.i(TAG, "Successfully sent the user a reset password link to $emailAddress");
   } else {
       Log.e(TAG, "Failed to send the user a reset password link to $emailAddress");
   }
});

Password reset emails contain two values, token and tokenId. To complete the password reset flow, prompt the user to enter a new password and pass the token and tokenId values along with the new password value to your Realm App’s EmailPasswordAuth’s resetPassword() or resetPasswordAsync() methods:

// token and tokenId are query parameters in the confirmation
// link sent in the password reset email.
app.emailPasswordAuth.resetPasswordAsync(token, tokenId, newPassword) {
   if (it.isSuccess) {
       Log.i(TAG, "Successfully updated password for user.")
   } else {
       Log.e(TAG, "Failed to reset user's password.")
   }
}
// token and tokenId are query parameters in the confirmation
// link sent in the password reset email.
app.getEmailPasswordAuth().resetPasswordAsync(token, tokenId, newPassword, it -> {
   if (it.isSuccess()) {
       Log.i(TAG, "Successfully updated password for user.");
   } else {
       Log.e(TAG, "Failed to reset user's password.");
   }
});

Note

To access the token and tokenId values sent in the password reset email, you can use a custom password reset email subject containing a deep link.