Navigation

Authenticate a User

Overview

The Node SDK provides developers with a unified API to authenticate application users for any authentication provider. Users log in by providing authentication credentials for a given authentication provider and the SDK automatically manages authentication tokens and refreshes data for logged in users.

MongoDB Realm provides developers with an API to log application users in and out with any enabled authentication provider. Pass in a credentials object to specify the authentication provider info to the login methods.

The SDK provides the following methods for user authentication:

Method Usage
App.logIn() Call App.logIn() to log a user in with a Realm.Credentials object for an enabled authentication provider. For example, running the script: “app.logIn(Realm.Credentials.emailPassword(‘<email>’, ‘<password>’))” would log in a user created with an email/password authentication provider.
User.logOut() Call User.logOut() to log a user out, regardless of the authentication provider the user was registered using.

Log In

Anonymous

The Anonymous provider allows users to log in to your application with short-term accounts that have no associated information. To do this, create an anonymous credential by calling “Realm.Credentials.anonymous()” and then pass in that credential to “App.logIn()”.

The anonymous authentication provider allows users to log in to your application with short-term accounts that have no associated information.

To log in an anonymous user, create an AnonymousCredential and pass it to App.logIn():

async function loginAnonymous() {
  // Create an anonymous credential
  const credentials = Realm.Credentials.anonymous();
  try {
    // Authenticate the user
    const user: Realm.User = await app.logIn(credentials);
    // `App.currentUser` updates to match the logged in user
    assert(user.id === app.currentUser.id)
    return user
  } catch(err) {
    console.error("Failed to log in", err);
  }
}
loginAnonymous().then(user => {
  console.log("Successfully logged in!", user)
})
async function loginAnonymous() {
  // Create an anonymous credential
  const credentials = Realm.Credentials.anonymous();
  try {
    // Authenticate the user
    const user = await app.logIn(credentials);
    // `App.currentUser` updates to match the logged in user
    assert(user.id === app.currentUser.id)
    return user
  } catch(err) {
    console.error("Failed to log in", err);
  }
}
loginAnonymous().then(user => {
  console.log("Successfully logged in!", user)
})

Email/Password

The email/password authentication provider allows users to log in to your application with an email address and a password.

To log in an email/password user, create an EmailPasswordCredential with the user’s email address and password and pass it to App.logIn():

async function loginEmailPassword(email: string, password: string) {
  // Create an anonymous credential
  const credentials = Realm.Credentials.emailPassword(email, password);
  try {
    // Authenticate the user
    const user: Realm.User = await app.logIn(credentials);
    // `App.currentUser ` updates to match the logged in user
    assert(user.id === app.currentUser.id)
    return user
  } catch(err) {
    console.error("Failed to log in", err);
  }
}
loginEmailPassword("cal.kestis@example.com", "Bogano123!").then(user => {
  console.log("Successfully logged in!", user)
})
async function loginEmailPassword(email, password) {
  // Create an anonymous credential
  const credentials = Realm.Credentials.emailPassword(email, password);
  try {
    // Authenticate the user
    const user = await app.logIn(credentials);
    // `App.currentUser` updates to match the logged in user
    assert(user.id === app.currentUser.id)
    return user
  } catch(err) {
    console.error("Failed to log in", err);
  }
}
loginEmailPassword("cal.kestis@example.com", "Bogano123!").then(user => {
  console.log("Successfully logged in!", user)
})

API Key

The API key authentication provider allows server processes to access to access your app directly or on behalf of a user.

To log in an API key user, create an ApiKeyCredential with a server or user API key and pass it to App.logIn():

async function loginApiKey(apiKey: string) {
  // Create an APIKey credential
  const credentials = Realm.Credentials.userAPIKey(apiKey);
  try {
    // Authenticate the user
    const user: Realm.User = await app.logIn(credentials);
    // `App.currentUser` updates to match the logged in user
    assert(user.id === app.currentUser.id)
    return user
  } catch(err) {
    console.error("Failed to log in", err);
  }
}
loginApiKey("To0SQOPC...ZOU0xUYvWw").then(user => {
  console.log("Successfully logged in!", user)
})
async function loginApiKey(apiKey) {
  // Create an APIKey credential
  const credentials = Realm.Credentials.userAPIKey(apiKey);
  try {
    // Authenticate the user
    const user = await app.logIn(credentials);
    // `App.currentUser` updates to match the logged in user
    assert(user.id === app.currentUser.id)
    return user
  } catch(err) {
    console.error("Failed to log in", err);
  }
}
loginApiKey("To0SQOPC...ZOU0xUYvWw").then(user => {
  console.log("Successfully logged in!", user)
})

Log Out

To log any user out, call the User.logOut() on their user instance.

// Log out the current user
await app.currentUser.logOut();
// Log out a specific user
await app.allUsers[2].logOut();
// Log out the current user
await app.currentUser.logOut();
// Log out a specific user
await app.allUsers[2].logOut();