Manage Email/Password Users - Java SDK
On this page
When you have enabled the email/password provider in your 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.getEmailPassword().registerUserAsync(email, password, it -> { if (it.isSuccess()) { Log.i("EXAMPLE", "Successfully registered user."); } else { Log.e("EXAMPLE", "Failed to register user: " + it.getError().getErrorMessage()); } });
app.emailPassword.registerUserAsync(email, password) { if (it.isSuccess) { Log.i("EXAMPLE","Successfully registered user.") } else { Log.e("EXAMPLE","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.getEmailPassword().confirmUserAsync(token, tokenId, it -> { if (it.isSuccess()) { Log.i("EXAMPLE", "Successfully confirmed new user."); } else { Log.e("EXAMPLE", "Failed to confirm user: " + it.getError().getErrorMessage()); } });
// token and tokenId are query parameters in the confirmation // link sent in the confirmation email. app.emailPassword.confirmUserAsync(token, tokenId) { if (it.isSuccess) { Log.i("EXAMPLE", "Successfully confirmed new user.") } else { Log.e("EXAMPLE", "Failed to register user: ${it.error}") } }
Tip
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 password in Sync, you can either:
Send a password reset email
Run a password reset function
Select your preferred password reset method by going to:
Your App
Authentication
Authentication Providers
Email/Password - and press the EDIT button
Send a Password Reset Email
To reset a user's password, first send the user a password reset email with sendResetPasswordEmail() or sendResetPasswordEmailAsync():
app.getEmailPassword().sendResetPasswordEmailAsync(email, it -> { if (it.isSuccess()) { Log.i("EXAMPLE", "Successfully sent the user a reset password link to " + email); } else { Log.e("EXAMPLE", "Failed to send the user a reset password link to " + email + ": " + it.getError().getErrorMessage()); } });
app.emailPassword.sendResetPasswordEmailAsync(email) { if (it.isSuccess) { Log.i("EXAMPLE", "Successfully sent the user a reset password link to $email") } else { Log.e("EXAMPLE", "Failed to send the user a reset password link to $email: $it.error") } }
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
instance's resetPassword()
or resetPasswordAsync()
methods:
// token and tokenId are query parameters in the confirmation // link sent in the password reset email. app.getEmailPassword().resetPasswordAsync(token, tokenId, newPassword, it -> { if (it.isSuccess()) { Log.i("EXAMPLE", "Successfully updated password for user."); } else { Log.e("EXAMPLE", "Failed to reset user's password: " + it.getError().getErrorMessage()); } });
// token and tokenId are query parameters in the confirmation // link sent in the password reset email. app.emailPassword.resetPasswordAsync(token, tokenId, newPassword) { if (it.isSuccess) { Log.i("EXAMPLE", "Successfully updated password for user.") } else { Log.e("EXAMPLE", "Failed to reset user's password: $it.error") } }
Tip
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.
Run a Password Reset Function
When you configure your app to run a password reset function, you'll define the function that should run when you call callResetPasswordFunction() or callResetPasswordFunctionAsync() from the SDK. This function can take a username, a password, and any number of additional arguments. You can use these arguments to specify details like security question answers or other challenges that the user should pass to successfully complete a password reset.
You might prefer to use a custom password reset function when you want to define your own password reset flows. For example, you might send a custom password reset email from a specific domain, or through a service other than email.
Tip
See also:
For more information on how to define a custom password reset function in your App, see: Run a Password Reset Function.
String newPassword = "newFakePassword"; String[] args = {"security answer 1", "security answer 2"}; app.getEmailPassword().callResetPasswordFunctionAsync(email, newPassword, args, it -> { if (it.isSuccess()) { Log.i("EXAMPLE", "Successfully reset the password for" + email); } else { Log.e("EXAMPLE", "Failed to reset the password for" + email + ": " + it.getError().getErrorMessage()); } });
val newPassword = "newFakePassword" val args = arrayOf("security answer 1", "security answer 2") app.emailPassword.callResetPasswordFunctionAsync(email, newPassword, args) { if (it.isSuccess) { Log.i("EXAMPLE", "Successfully reset the password for $email") } else { Log.e("EXAMPLE", "Failed to reset the password for $email: $it.error") } }