Navigation

Authenticate a User

Overview

Before you can authenticate a user, ensure you have:

Log In

Anonymous Authentication

When you have enabled anonymous authentication in the Realm UI, users can immediately log into your app without providing any identifying information:

app.login(withCredential: AppCredentials.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

When you have enabled email/password authentication, you can log in:

let username = "skroob@example.com"
let password = "12345"
app.login(withCredential: AppCredentials(username: username, password: password)) { (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:

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