Docs Menu

Manage Email/Password Users - Swift SDK

On this page

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

Changed in version 10.16.0: Email/password user APIs add async/await support. Code examples on this page updated to async/await syntax. For an example of the older syntax, see: Email/Password User Examples with Completion Handlers.

The SDK provides methods to resend user confirmation emails or retry custom confirmation methods.

Resend a confirmation email. Email/password URLs expire after 30 minutes, so users who do not visit within that period will need new emails to confirm their accounts.

let app = App(id: YOUR_APP_SERVICES_APP_ID)
let client = app.emailPasswordAuth
let email = ""
// 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)")

New in version 10.9.0.

Retry a custom user confirmation function.

let app = App(id: YOUR_APP_SERVICES_APP_ID)
let client = app.emailPasswordAuth
let email = ""
// 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)")

To reset a user password in Device Sync, you can either:

  • Send a password reset email
  • Run a password reset function

Select your preferred password reset method by going to:

  1. Your Atlas App Services App
  2. Authentication
  3. Authentication Providers
  4. Email/Password - and press the EDIT button

After you configure your App to send a password reset email, you can call sendResetPasswordEmail with the user's email. App Services sends an email to the user that contains a unique URL. The user must visit this URL within 30 minutes to confirm the reset.

When you configure your app to run a password reset function, you'll define the function that should run when you call callResetPasswordFunction() 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.

See also:

For more information on how to define a custom password reset function in the Device Sync backend, including how to structure it and examples of implementing custom flows, see: Run a Password Reset Function.

let app = App(id: YOUR_APP_SERVICES_APP_ID)
let client = app.emailPasswordAuth
let email = ""
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)")

If you're not using Apple's async/await syntax, all of these methods are available with completion handlers. This example shows registering a user with the completion handler syntax.

let app = App(id: YOUR_APP_SERVICES_APP_ID)
let client = app.emailPasswordAuth
let email = ""
let password = "password12345"
client.registerUser(email: email, password: password) { (error) in
guard error == nil else {
print("Failed to register: \(error!.localizedDescription)")
// Registering just registers. You can now log in.
print("Successfully registered user.")
←  User Metadata - Swift SDKMulti-User Applications - Swift SDK →
Give Feedback
© 2022 MongoDB, Inc.


  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2022 MongoDB, Inc.