Docs Menu

Authenticate Users - Flutter SDK

On this page

  • Before You Begin
  • Register a New User Account
  • Log In
  • Anonymous User
  • Email/Password User
  • Log a User Out
  • Retrieve Current User

Atlas App Services provides an API for authenticating users using any enabled authentication provider. Instantiate a Credentials object and pass it to App.logIn() to authenticate and create a User object for that user. Each authentication provider corresponds to a static constructor method used to instantiate Credentials objects for that authentication provider.

  1. Create an Atlas App Services app.
  2. Enable and configure one or more App Services authentication providers.

Realm registers accounts differently depending on the authentication provider:

  • You do not need to register anonymous users.
  • To register an email/password user, refer to Email/Password User Registration.
  • If you are using Google, Facebook, Apple, or Custom JWT authentication, registration is handled by these third-party services.

You can authenticate users with App.logIn().

If successful, app.logIn returns a User object.

The anonymous authentication provider enables users to log in to your application with short-term accounts that store no persistent personal information. To log in with anonymous authentication, create an anonymous credential by calling Credentials.anonymous() and then pass the generated credential to app.logIn.

Credentials anonCredentials = Credentials.anonymous();
await app.logIn(anonCredentials);

The Email/Password authentication provider enables users to log in to your application with an email username and a password. To log in with email/password authentication, create an email/password credential by calling Credentials.emailPassword() with the user's email and password. Then pass the generated credential to app.logIn.

Credentials emailPwCredentials =
Credentials.emailPassword("lisa@example.com", "myStr0ngPassw0rd");
await app.logIn(emailPwCredentials);
Tip
See also:

To learn more about the complete flow of using App Services Email/Password authentication, refer to the Email/Password Users documentation.

You can log out any user, regardless of the authentication provider used to log in, using User.logOut(). This method:

  • Deletes locally stored user credentials from the device
  • Immediately halts any synchronization to and from the user's realms

Because logging out halts synchronization, you should only log out after all local Realm updates have uploaded to the server.

await user.logOut();

Once you have an authenticated user, you can retrieve the User object with the App.currentUser property. The currentUser object is persisted in local storage, so even if the app shuts down after the initial authentication, you do not need to call logIn again (unless the user logged out).

User? user = app.currentUser;
←  Users - Flutter SDKEmail/Password Users - Flutter SDK →
Give Feedback
© 2022 MongoDB, Inc.

About

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