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.
Register a New User Account
Confirm a New User's Email Address
Retry User Confirmation Methods
The SDK provides methods to resend user confirmation emails or retry custom confirmation methods.
Resend a User Confirmation Email
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 = "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)") }
Retry a User Confirmation Function
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 = "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)") }
Reset a User's Password
To reset a user password in Atlas Device Sync, you can either:
- Send a password reset email
- Run a password reset function
Select your preferred password reset method by going to:
- Your Atlas App Services App
- Authentication
- Authentication Providers
- Email/Password - and press the EDIT button
Send a Password Reset Email
After you configure your App to send a password reset email, you can call sendResetPasswordEmail
with the user's email. Atlas 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.
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() 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.
For more information on how to define a custom password reset function in the Atlas 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 = "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)") }
Email/Password User Methods with Completion Handlers
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 = "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.") }