Docs 菜单
Docs 主页
/ /
Atlas Device SDKs
/ /

管理电子邮件/密码用户 — Java SDK

在此页面上

  • 注册新用户帐户
  • 确认新用户的电子邮件地址
  • 重置用户密码
  • 发送密码重置电子邮件
  • 运行密码重置功能

在应用程序中启用电子邮件/密码提供商后,您可以注册新帐户,确认电子邮件解决,并从客户端代码中重置用户的密码。

要注册新用户,请将用户提供的电子邮件和密码传递给 RealmApp EmailPasswordAuth 实例的 registerUser() 或 registerUserAsync() 方法:

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}")
}
}

要确认新创建的用户,请将确认tokentokenId 传递给RealmApp EmailPasswordAuth 实例的确认用户 () 或确认用户 Async() 方法:

// 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}")
}
}

提示

要在 Sync 中重置用户密码,您可以:

  • 发送密码重置电子邮件

  • 运行密码重置功能

选择您首选的密码重置方法:

  1. 您的应用程序

  2. Authentication

  3. Authentication Providers

  4. Email/Password - 然后按EDIT按钮

要重置用户的密码,请先使用sendResetPasswordEmail()或 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")
}
}

密码重置电子邮件包含两个值: tokentokenId 。 要完成密码重置流程,请提示用户输入新密码,并将tokentokenId值以及新密码值传递给Realm AppEmailPasswordAuth实例的resetPassword()resetPasswordAsync()方法:

// 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")
}
}

提示

要访问密码重置电子邮件中发送的 token和 值,您可以使用包含tokenId 深层链接 自定义密码重置电子邮件主题 。

当您将应用配置为运行密码重置函数时,您将定义当您从 SDK 调用callResetPasswordFunction()callResetPasswordFunctionAsync()时应运行的函数。 此函数可以接受用户名、密码和任意数量的其他参数。 您可以使用这些参数指定用户应传递以成功完成密码重置的详细信息,例如安全问题答案或其他挑战。

如果要定义自己的密码重置流程,您可能更愿意使用自定义密码重置函数。 例如,您可以从特定域或通过电子邮件以外的服务发送自定义密码重置电子邮件。

提示

另请参阅:

有关如何在应用中定义自定义密码重置函数的更多信息,请参阅:运行密码重置函数。

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")
}
}

后退

自定义用户数据