Authenticate a User - Web SDK
On this page
Overview
The Web 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.
Log In
Anonymous
The Anonymous provider allows users to log in to your application with ephemeral accounts that have no associated information.
To log in, create an anonymous credential and pass it to App.logIn()
:
async function loginAnonymous() { // Create an anonymous credential const credentials = Realm.Credentials.anonymous(); // Authenticate the user const user = await app.logIn(credentials); // `App.currentUser` updates to match the logged in user console.assert(user.id === app.currentUser.id); return user; } const user = await loginAnonymous();
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, create an email/password credential with the user's email address and
password and pass it to App.logIn()
:
async function loginEmailPassword(email, password) { // Create an email/password credential const credentials = Realm.Credentials.emailPassword(email, password); // Authenticate the user const user = await app.logIn(credentials); // `App.currentUser` updates to match the logged in user console.assert(user.id === app.currentUser.id); return user; } const user = await loginEmailPassword("joe.jasper@example.com", "passw0rd");
API Key
The API key authentication provider allows server processes to access your app directly or on behalf of a user.
To log in with an API key, create an API Key credential with a server or user
API key and pass it to App.logIn()
:
async function loginApiKey(apiKey) { // Create an API Key credential const credentials = Realm.Credentials.apiKey(apiKey); // Authenticate the user const user = await app.logIn(credentials); // `App.currentUser` updates to match the logged in user console.assert(user.id === app.currentUser.id); return user; } const user = await loginApiKey(REALM_API_KEY.key); // add previously generated API key
Custom Function
The Custom Function authentication provider allows you to handle user authentication by running a function that receives a payload of arbitrary information about a user.
To log in with the custom function provider, create a Custom Function credential
with a payload object and pass it to App.logIn()
:
async function loginCustomFunction(payload) { // Create a Custom Function credential const credentials = Realm.Credentials.function(payload); // Authenticate the user const user = await app.logIn(credentials); // `App.currentUser` updates to match the logged in user console.assert(user.id === app.currentUser.id); return user; } const user = await loginCustomFunction({ username: "ilovemongodb" });
Custom JWT
The Custom JWT authentication provider allows you to handle user authentication with any authentication system that returns a JSON web token.
To log in, create a Custom JWT credential with a JWT from the external system
and pass it to App.logIn()
:
async function loginCustomJwt(jwt) { // Create a Custom JWT credential const credentials = Realm.Credentials.jwt(jwt); // Authenticate the user const user = await app.logIn(credentials); // `App.currentUser` updates to match the logged in user console.assert(user.id === app.currentUser.id); return user; } const user = await loginCustomJwt("eyJ0eXAi...Q3NJmnU8oP3YkZ8");
Facebook Authentication
The Facebook authentication provider allows you to authenticate users through a Facebook app using their existing Facebook account. You can use the built-in authentication flow or authenticate with the Facebook SDK.
Important
Enable the Facebook Auth Provider
To log a user in with their existing Facebook account, you must configure and enable the Facebook authentication provider for your application.
Important
Do Not Store Facebook Profile Picture URLs
Facebook profile picture URLs include the user's access token to grant permission to the image. To ensure security, do not store a URL that includes a user's access token. Instead, access the URL directly from the user's metadata fields when you need to fetch the image.
Use the Built-In Facebook OAuth 2.0 Flow
The Realm Web SDK includes methods to handle the OAuth 2.0 process and does not require you to install the Facebook SDK. The built in flow follows three main steps:
You call
app.logIn()
with a Facebook credential. The credential must specify a URL for your app that is also listed as a redirect URI in the provider configuration.// The redirect URI should be on the same domain as this app and // specified in the auth provider configuration. const redirectUri = "https://app.example.com/handleOAuthLogin"; const credentials = Realm.Credentials.facebook(redirectUri); // Calling logIn() opens a Facebook authentication screen in a new window. const user = await app.logIn(credentials); // The app.logIn() promise will not resolve until you call `Realm.handleAuthRedirect()` // from the new window after the user has successfully authenticated. console.log(`Logged in with id: ${user.id}`); A new window opens to a Facebook authentication screen and the user authenticates and authorizes your app in that window. Once complete, the new window redirects to the URL specified in the credential.
You call
Realm.handleAuthRedirect()
on the redirected page, which stores the user's Atlas App Services access token and closes the window. Your original app window will automatically detect the access token and finish logging the user in.// When the user is redirected back to your app, handle the redirect to // save the user's access token and close the redirect window. This // returns focus to the original application window and automatically // logs the user in. Realm.handleAuthRedirect();
Authenticate with the Facebook SDK
You can use the official Facebook SDK to handle the user authentication and redirect flow. Once authenticated, the Facebook SDK returns an access token that you can use to finish logging the user in to your app.
// Get the access token from the Facebook SDK const { accessToken } = FB.getAuthResponse(); // Define credentials with the access token from the Facebook SDK const credentials = Realm.Credentials.facebook(accessToken); // Log the user in to your app await app.logIn(credentials);
Google Authentication
The Google authentication provider allows you to authenticate users through a Google project using their existing Google account.
Note
Enable the Google Auth Provider
To authenticate a Google user, you must configure the Google authentication provider.
Logging a Google user in to your App is a two step process:
First, you authenticate the user with Google. You can use a library like Google One Tap for a streamlined experience.
Second, you log the user in to your App with an authentication token returned by Google upon successful user authentication.
Authenticate with Google One Tap
Note
Requires OpenID Connect
Google One Tap only supports user authentication through OpenID Connect. To log Google users in to your web app, you must enable OpenID Connect in the App Services authentication provider configuration.
Google One Tap is a Google SDK that lets users sign up to or log in to a website with one click (or tap).
Example
Basic Google One Tap Flow
This example shows how to set up Google Authentication with App Services in
a very basic web app. The app only contains an index.html
file.