Authenticate Users - Flutter SDK
On this page
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.
Before You Begin
- Create an Atlas App Services app.
- Enable and configure one or more App Services authentication providers.
Register a New User Account
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.
Log In
You can authenticate users with App.logIn().
If successful, app.logIn
returns a User
object.
Anonymous User
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);
Email/Password User
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);
To learn more about the complete flow of using App Services Email/Password authentication, refer to the Email/Password Users documentation.
Log a User Out
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();
Retrieve Current User
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;