Docs Menu

Docs HomeRealm

Authenticate Users - C++ SDK (Alpha)

On this page

  • Log In
  • Anonymous User
  • Email/Password User
  • API Key User
  • Custom Function User
  • Custom JWT User
  • Facebook User
  • Google User
  • Apple User
  • Get a User Access Token
  • Log Out

If you have enabled Anonymous authentication in the App Services UI, users can immediately log into your app without providing any identifying information.

auto app = realm::App(APP_ID);
auto user = app.login(realm::App::credentials::anonymous()).get_future().get();

If you have enabled Email/Password authentication, and have registered an email/password user, you can log that user in.

auto user = app.login(realm::App::credentials::username_password(userEmail, userPassword))
.get_future().get();

If you have enabled API Key authentication, you can log in using a user API key.

auto app = realm::App(APP_ID);
auto user = app.login(realm::App::credentials::api_key(API_KEY)).get_future().get();

If you have enabled the Custom Function authentication provider, you can log in using a custom function.

// Custom function authentication takes a BSON Document with parameters.
// The parameter details vary depending on how you define your custom authentication function.
realm::bson::BsonDocument params = {{ "username", "bob" }};
auto app = realm::App(APP_ID);
auto user = app.login(realm::App::credentials::function(params)).get_future().get();

If you have enabled the Custom JWT authentication provider, you can log in using a custom JWT.

auto token = "<jwt>";
auto app = realm::App(APP_ID);
auto user = app.login(realm::App::credentials::custom(token)).get_future().get();

The Facebook authentication provider allows you to authenticate users through a Facebook app using their existing Facebook account.

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.

auto app = realm::App(APP_ID);
auto accessToken = "<token>";
auto user = app.login(realm::App::credentials::facebook(accessToken)).get_future().get();

If you have configured the Google authentication provider, you can log in using an existing Google account.

To log in with a Google authentication code, pass a Google authentication code to credentials::google(auth_code).

auto app = realm::App(APP_ID);
// The auth_code below is the user's server auth code you got from Google
auto user = app.login(realm::App::credentials::google(authCode)).get_future().get();

To log in with a Google ID token, pass a Google ID token to credentials::google(id_token).

auto app = realm::App(APP_ID);
// The id_token below is the user's OpenID Connect id_token you got from the Google OAuth response
auto user = app.login(realm::App::credentials::google(idToken)).get_future().get();

If you have enabled Sign-in with Apple authentication, you can log a user in using an Apple ID token.

auto app = realm::App(APP_ID);
auto idToken = "<token>";
auto user = app.login(realm::App::credentials::apple(idToken)).get_future().get();

The Realm SDK automatically manages access tokens, refreshes them when they expire, and includes a valid access token for the current user with each request.

If you send requests outside of the SDK - for example, through the GraphQL API - then you must include the user's access token with each request. In this scenario, you must manually refresh the token when it expires. Access tokens expire after 30 minutes.

You can call .refresh_custom_user_data() on a logged-in user to refresh the user's auth session. Then, get the .access_token() as a string you can use in your code. You might use code similar to this to fetch an access token:

// With a logged-in user, refresh the custom user data to refresh the auth session
user.refresh_custom_user_data().get_future().get();
// Then get the user's access token
auto userAccessToken = user.access_token();

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.

user.log_out().get_future().get();
←  Manage Users - C++ SDK (Alpha)Manage Email/Password Users - C++ SDK →
Share Feedback
© 2023 MongoDB, Inc.

About

  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2023 MongoDB, Inc.