“文档” 菜单
文档首页
/ /
Atlas Device SDKs
/ /

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

在此页面上

  • 注册新用户帐户
  • 确认新用户的电子邮件地址
  • 重试用户确认方法
  • 重新发送用户确认电子邮件
  • 重试用户确认函数
  • 重置用户密码
  • 发送密码重置电子邮件
  • 运行密码重置功能
  • 带有完成处理程序的电子邮件/密码用户方法

在 Atlas App Services 应用中启用电子邮件/密码提供商后,您可以注册新帐户、确认电子邮件地址以及从客户端代码重置用户密码。

在版本10中进行了更改。 16 。 0 :电子邮件/密码用户 API 添加了异步/等待支持。此页面上的代码示例已更新为异步/等待语法。有关旧语法的示例,请参阅: 使用完成处理程序的电子邮件/密码用户示例。

RLMApp *app = [RLMApp appWithId:YOUR_APP_ID];
RLMEmailPasswordAuth *client = [app emailPasswordAuth];
NSString *email = @"skroob2@example.com";
NSString *password = @"password12345";
[client registerUserWithEmail:email password:password completion:^(NSError *error) {
if (error != nil) {
NSLog(@"Failed to register: %@", [error localizedDescription]);
return;
}
// Registering just registers. You can now log in.
NSLog(@"Successfully registered user.");
}];
let app = App(id: YOUR_APP_SERVICES_APP_ID)
let client = app.emailPasswordAuth
let email = "skroob@example.com"
let password = "password12345"
do {
try await client.registerUser(email: email, password: password)
// Registering just registers. You can now log in.
print("Successfully registered user.")
} catch {
print("Failed to register: \(error.localizedDescription)")
}
RLMApp *app = [RLMApp appWithId:YOUR_APP_ID];
RLMEmailPasswordAuth *client = [app emailPasswordAuth];
// Token and tokenId are query parameters in the confirmation
// link sent in the confirmation email.
NSString *token = @"someToken";
NSString *tokenId = @"someTokenId";
[client confirmUser:token tokenId:tokenId completion:^(NSError *error) {
if (error != nil) {
NSLog(@"User confirmation failed: %@", [error localizedDescription]);
return;
}
// User confirmed
NSLog(@"Successfully confirmed user.");
}];
let app = App(id: YOUR_APP_SERVICES_APP_ID)
let client = app.emailPasswordAuth
// Token and tokenId are query parameters in the confirmation
// link sent in the confirmation email.
let token = "someToken"
let tokenId = "someTokenId"
do {
try await client.confirmUser(token, tokenId: tokenId)
// User email address confirmed.
print("Successfully confirmed user.")
} catch {
print("User confirmation failed: \(error.localizedDescription)")
}

SDK 提供了各种方法,允许重新发送用户确认邮件或重试自定义确认方法。

重新发送确认电子邮件。 每个 URL 中的确认令牌会在30分钟后过期。 如果用户没有在该期限内点击链接并确认,则必须请求新的确认电子邮件。

let app = App(id: YOUR_APP_SERVICES_APP_ID)
let client = app.emailPasswordAuth
let email = "skroob@example.com"
// If Realm is set to send a confirmation email, we can
// send the confirmation email again here.
do {
try await client.resendConfirmationEmail(email)
// The confirmation email has been sent
// to the user again.
print("Confirmation email resent")
} catch {
print("Failed to resend confirmation email: \(error.localizedDescription)")
}

版本 10.9.0 中的新增内容

重试自定义用户确认函数。

let app = App(id: YOUR_APP_SERVICES_APP_ID)
let client = app.emailPasswordAuth
let email = "skroob@example.com"
// If Realm is set to run a custom confirmation function,
// we can trigger that function to run again here.
do {
try await client.retryCustomConfirmation(email)
// The custom confirmation function has been
// triggered to run again for the user.
print("Custom confirmation retriggered")
} catch {
print("Failed to retry custom confirmation: \(error.localizedDescription)")
}

重置用户密码是一个多步骤的过程。

  1. 在客户端应用程序中,您为用户提供一个用于重置密码的用户界面。 然后,您的 App Services App 可以发送电子邮件或运行自定义函数来确认用户的身份。

  2. 确认用户身份后,即可完成密码重置请求。

  3. 密码重置完成后,用户可以使用新密码登录。

有关如何设置首选密码重置方法的更多信息,请参阅Atlas App Services电子邮件/密码身份验证文档。

要发送密码重置电子邮件以确认用户身份,您必须将应用配置为发送密码重置电子邮件。

要开始密码重置过程,请使用用户的电子邮件调用 sendResetPasswordEmail 。 App Services 会向用户发送一封包含唯一 URL 的电子邮件。 用户必须在 30 分钟内访问此 URL 才能确认重置。

在用户访问密码重置电子邮件中的 URL 后,使用用户的电子邮件、新密码以及唯一 URL 中提供的tokentokenId调用resetPassword

如果用户在 30 分钟内没有访问密码重置电子邮件中的 URL, tokentokenId则会过期。 您必须再次开始密码重置过程。

RLMApp *app = [RLMApp appWithId:YOUR_APP_ID];
RLMEmailPasswordAuth *client = [app emailPasswordAuth];
// If Realm app password reset mode is "Send a password reset email",
// we can do so here:
NSString *email = @"forgot.my.password@example.com";
[client sendResetPasswordEmail:email completion:^(NSError *error) {
if (error != nil) {
NSLog(@"Failed to send reset password email: %@", [error localizedDescription]);
return;
}
// Email sent.
NSLog(@"Successfully sent reset password email.");
}];
// Later...
NSString *newPassword = @"mynewpassword12345";
// Token and tokenId are query parameters in the confirmation
// link sent in the reset password email.
NSString *token = @"someToken";
NSString *tokenId = @"someTokenId";
[client resetPasswordTo:newPassword token:token tokenId:tokenId completion:^(NSError *error) {
if (error != nil) {
NSLog(@"Failed to reset password: %@", [error localizedDescription]);
return;
}
// Password reset.
NSLog(@"Successfully reset password.");
}];
let app = App(id: YOUR_APP_SERVICES_APP_ID)
let client = app.emailPasswordAuth
let email = "forgot.my.password@example.com"
// If Realm app password reset mode is "Send a password reset email",
// we can do so here:
do {
try await client.sendResetPasswordEmail(email)
print("Password reset email sent.")
} catch {
print("Reset password email not sent: \(error.localizedDescription)")
}
// Later...
let newPassword = "mynewpassword12345"
// Token and tokenId are query parameters in the reset password
// link sent in the reset password email.
let token = "someToken"
let tokenId = "someTokenId"
do {
try await client.resetPassword(to: newPassword, token: token, tokenId: tokenId)
print("Password reset successful.")
} catch {
print("Failed to reset password: \(error.localizedDescription)")
}

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

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

在 App Services 端,您可以定义在调用此方法时运行的自定义密码重置函数。 该函数可以返回三种可能的状态之一:

  • fail

  • pending

  • success

SDK 将fail状态视为错误。 SDK callResetPasswordFunction()不接受返回值,因此它不会向客户端返回pendingsuccess状态。

如果您希望用户采取一些额外步骤来确认其身份,您的Atlas App Services密码重置函数可能会返回 pending。 但是,该返回值不会传递给 SDK 的callResetPasswordFunction() ,因此您的客户端应用程序必须实现自己的逻辑来处理pending状态。

您的服务器端函数可能会使用自定义电子邮件提供商发送电子邮件。 或者,您也可以使用短信,通过短信确认用户的身份。

您可以在Atlas App Services密码重置函数上下文中使用 tokentokenId。 如果您通过 密码重置功能传递此信息,则可以使用Atlas App Services 通用链接 将这些值传递回您的应用程序 。然后,客户端应用程序可以调用resetPassword来完成密码重置流程。

let app = App(id: YOUR_APP_SERVICES_APP_ID)
let client = app.emailPasswordAuth
let email = "forgot.my.password@example.com"
let newPassword = "mynewpassword12345"
// The password reset function takes any number of
// arguments.
let args: [AnyBSON] = []
// This SDK call maps to the custom password reset
// function that you define in the backend. In this example,
// we assume your function waits for additional identity
// confirmation. Calling this function only kicks
// off the password reset function. It does not reset the password.
do {
try await client.callResetPasswordFunction(email: email, password: newPassword, args: args)
print("Successfully called the password reset function")
} catch {
print("Password reset failed: \(error.localizedDescription)")
}
// Later...
// Token and tokenId are parameters you can access
// in the App Services function context. You could send
// this to the user via email, SMS, or some other method.
let token = "someToken"
let tokenId = "someTokenId"
do {
try await client.resetPassword(to: newPassword, token: token, tokenId: tokenId)
print("Password reset successful.")
} catch {
print("Failed to reset password: \(error.localizedDescription)")
}

如果Atlas App Services密码重置函数在函数内进行了额外验证,或者如果您在尝试重置密码之前已验证用户身份,则可以将Atlas App Services函数配置为返回 success。 但是,该返回值不会传递给 SDK 的callResetPasswordFunction() ,因此您的客户端应用程序必须实现自己的逻辑来处理success状态。

调用本示例中的函数会执行整个密码重置过程。

let app = App(id: YOUR_APP_SERVICES_APP_ID)
let client = app.emailPasswordAuth
let email = "forgot.my.password@example.com"
let newPassword = "mynewpassword12345"
// The password reset function takes any number of
// arguments. You might ask the user to provide answers to
// security questions, for example, to verify the user
// should be able to complete the password reset.
let args: [AnyBSON] = []
// This SDK call maps to the custom password reset
// function that you define in the backend
do {
try await client.callResetPasswordFunction(email: email, password: newPassword, args: args)
print("Password reset successful!")
} catch {
print("Password reset failed: \(error.localizedDescription)")
}

如果您使用的不是 Apple 的 async/await 语法 ,所有这些方法都可用于完成处理程序。此示例显示了使用完成处理程序语法 注册用户

let app = App(id: YOUR_APP_SERVICES_APP_ID)
let client = app.emailPasswordAuth
let email = "skroob@example.com"
let password = "password12345"
client.registerUser(email: email, password: password) { (error) in
guard error == nil else {
print("Failed to register: \(error!.localizedDescription)")
return
}
// Registering just registers. You can now log in.
print("Successfully registered user.")
}

后退

用户元数据

来年

多用户应用程序