Navigation

Manage Email/Password Users

Overview

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

Register a New User Account

let app = App(id: YOUR_REALM_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.")
}
RLMApp *app = [RLMApp appWithId:YOUR_REALM_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.");
}];

Confirm a New User’s Email Address

let app = App(id: YOUR_REALM_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"
client.confirmUser(token, tokenId: tokenId) { (error) in
    guard error == nil else {
        print("User confirmation failed: \(error!.localizedDescription)")
        return
    }
    // User email address confirmed.
    print("Successfully confirmed user.")
}
RLMApp *app = [RLMApp appWithId:YOUR_REALM_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.");
}];

Reset a User’s Password

let app = App(id: YOUR_REALM_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:
client.sendResetPasswordEmail(email, completion: {(error) in
    guard error == nil else {
        print("Reset password email not sent: \(error!.localizedDescription)")
        return
    }
    print("Password reset email sent.")
})

// 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"
client.resetPassword(to: newPassword, token: token, tokenId: tokenId) { (error) in
    guard error == nil else {
        print("Failed to reset password: \(error!.localizedDescription)")
        return
    }
    // Password reset successful.
    print("Password reset successful.")
}
RLMApp *app = [RLMApp appWithId:YOUR_REALM_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.");
}];