Docs Menu

Authenticate Users - Kotlin SDK

On this page

  • Log In
  • Anonymous User
  • Email/Password User
  • Log a User Out

MongoDB Realm provides an API for authenticating users using any enabled authentication provider. Instantiate a Credentials object and pass it to app.login() method to authenticate a user and create a User object for that user. Each authentication provider corresponds to a static helper method used to instantiate Credentials objects for that authentication provider.

You can authenticate users with app.login().

If successful, app.login() returns a User object. In the event of a failure, app.login() throws an exception of type AppException.

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 AuthenticationProvider.ANONYMOUS() and then pass the generated credential to app.login().

val app: App = App.create(YOUR_APP_ID)
runBlocking {
val user = app.login(Credentials.anonymous())
}

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 AuthenticationProvider.EMAIL_PROVIDER() with the user's email and password. Then pass the generated credential to app.login().

val app: App = App.create(YOUR_APP_ID)
runBlocking {
val user = app.login(Credentials.emailPassword(email, password))
}

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.

user.logOut()
←  Register Users - Kotlin SDKMigrate from the Java SDK to the Kotlin SDK →
Give Feedback
© 2022 MongoDB, Inc.

About

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