Navigation

Authenticate a User

Overview

MongoDB Realm provides an API for authenticating users using any enabled authentication provider. Instantiate a Credentials object and pass it to the app.login() method to authenticate a user and create a User object. Each authentication provider corresponds to a method used to instantiate Credentials objects using that authentication provider. See the table below to find the method that instantiates the Credentials instance for your authentication provider:

Authentication Provider Credentials Generation Method
Anonymous Credentials.anonymous()
Email/Password Credentials(email: String, password: String)
API Key Credentials(userAPIKey: String)
Custom Function Credentials(functionPayload: [AnyHashable : Any], error: NSErrorPointer)
Custom JWT Credentials(jwt: String)
Google OAuth Credentials(googleToken: String)
Facebook OAuth Credentials(facebookToken: String)
Sign-in With Apple Credentials(appleToken: String)

Before you can authenticate a user, ensure you have:

Log In

Anonymous Authentication

If you have enabled Anonymous authentication in the Realm UI, users can immediately log into your app without providing any identifying information. The following code shows how to do this:

app.login(credentials: Credentials.anonymous()) { (user, error) in
     // Remember to dispatch back to the main thread in completion handlers
     // if you want to do anything on the UI.
     DispatchQueue.main.sync {
       guard error == nil else {
         print("Login failed: \(error!)")
         return
       }

       // Now logged in, do something with user

     }
}

Email/Password Authentication

If you have enabled Email/Password authentication, you can log in using the following code:

let email = "skroob@example.com"
let password = "12345"
app.login(credentials: Credentials(email: email, password: password)) { (user, error) in
    DispatchQueue.main.sync {
        guard error == nil else {
            print("Login failed: \(error!)")
            return
        }
        // Now logged in, do something with user
     }
}

API Key

If you have enabled API Key authentication, you can log in using the following code:

app.login(credentials: Credentials(userAPIKey: apiKey)) { (user, error) in
   DispatchQueue.main.sync {
       guard error == nil else {
           print("Login failed: \(error!)")
           return
       }
       // Now logged in, do something with user
   }
}

Custom Function

If you have enabled the Custom Function authentication provider, you can log in using the following code:

let params = [
   "username": "bob"
]

var e: NSError?

app.login(credentials: Credentials(functionPayload: params, error: &e)) { (user, error) in
   DispatchQueue.main.sync {
       guard error == nil else {
           print("Login failed: \(error!)")
           return
       }
       // Now logged in, do something with user
   }
}

Custom JWT

If you have enabled the Custom JWT authentication provider, you can log in using the following code:

app.login(credentials: Credentials(jwt: "<token>")) { (user, error) in
    DispatchQueue.main.sync {
        guard error == nil else {
            print("Login failed: \(error!)")
            return
        }
        // Now logged in, do something with user
    }
}

Facebook Authentication

If you have enabled Facebook authentication, you can log in using the following code:

// Fetch Facebook token via the Facebook SDK
app.login(credentials: Credentials(facebookToken: "<token>")) { (user, error) in
    DispatchQueue.main.sync {
        guard error == nil else {
            print("Login failed: \(error!)")
            return
        }
        // Now logged in, do something with user
    }
}

Google Authentication

If you have enabled Google authentication, you can log in using the following code:

// Fetch Google token via the Google SDK

app.login(credentials: Credentials(googleToken: "<token>")) { (user, error) in
   DispatchQueue.main.sync {
       guard error == nil else {
           print("Login failed: \(error!)")
           return
       }
       // Now logged in, do something with user
   }
}

Sign-in with Apple

If you have enabled Sign-in with Apple authentication, you can log in using the following code:

// Fetch Apple token via the Apple DSK

app.login(credentials: Credentials(appleToken: "<token>")) { (user, error) in
   DispatchQueue.main.sync {
       guard error == nil else {
           print("Login failed: \(error!)")
           return
       }
       // Now logged in, do something with user
   }
}

Log Out

Once logged in, you can log out:

Warning

When a user logs out, you can no longer read or write data in any synced realms that the user opened. As a result, any operation that has not yet completed before the initiating user logs out cannot complete successfully and will likely result in an error. Any data in a write operation that fails in this way will be lost.

app.currentUser()?.logOut(completion: { (error) in
    // Logged out or error occurred
})