Auth0.Android Login, Logout, and User Profiles

Add login to your Android application

You can log the user in using the WebAuthProvider.login method:

WebAuthProvider.login(account)
  .withScheme(getString(R.string.com_auth0_scheme))
  .withScope("openid profile email")
  .start(this, object : Callback<Credentials, AuthenticationException> {
    override fun onFailure(exception: AuthenticationException) {
       // Authentication failed
     }

      override fun onSuccess(credentials: Credentials) {
         // Authentication succeeded
       }
  })

The authentication result will be delivered to the onSuccess callback.

See the Auth0.Android configuration for more options for the WebAuthProvider class.

Add logout to your Android application

To log the user out, call the WebAuthProvider.logout method. The result of logout will be supplied in the onSuccess callback.

This method removes the cookie that the browser set at authentication time, so it forces users to re-enter their credentials the next time they try to authenticate.

If the logout is cancelled, you might want to take the user back to where they were before attempting to log out.

WebAuthProvider.logout(account)
  .withScheme("demo")
  .start(this, object: Callback<Void?, AuthenticationException> {
    override fun onSuccess(payload: Void?) {
      // The user has been logged out!
    }

    override fun onFailure(error: AuthenticationException) {
      // Something went wrong!
    }
  })

Show the user's profile

Use the AuthenticationAPIClient class to retrieve the user's profile from Auth0. This requires:

  • The access token as received during the login phase

  • The profile scope to include when WebAuthProvider.login is called

  • The email scope (if you want to retrieve the user's email address)

This sample demonstrates a function that retrieves the user's profile and displays it on screen:

var client = AuthenticationAPIClient(account)

// Use the received access token to call `userInfo` and get the profile from Auth0.
client.userInfo(accessToken)
  .start(object : Callback<UserProfile, AuthenticationException> {
      override fun onFailure(exception: AuthenticationException) {
          // Something went wrong!
      }

      override fun onSuccess(profile: UserProfile) {
        // We have the user's profile!
        val email = profile.email
        val name = profile.name
      }
})