管理电子邮件/密码用户 — 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}") } }
确认新用户的电子邮件地址
要确认新创建的用户,请将确认token
和tokenId
传递给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}") } }
提示
要访问权限用户确认电子邮件中发送的token
和tokenId
值,您可以使用包含 深层链接 的 自定义确认电子邮件主题 。
重置用户密码
要在 Sync 中重置用户密码,您可以:
发送密码重置电子邮件
运行密码重置功能
选择您首选的密码重置方法:
您的应用程序
Authentication
Authentication Providers
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") } }
密码重置电子邮件包含两个值: token
和tokenId
。 要完成密码重置流程,请提示用户输入新密码,并将token
和tokenId
值以及新密码值传递给Realm App
的EmailPasswordAuth实例的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") } }