Android: User Profile

View on Github

Android: User Profile

Gravatar for
By Luciano Balmaceda

This tutorial will show you how to get and modify the user's profile data. We recommend you to Log in to follow this quickstart with examples configured for your account.

I want to explore a sample app

2 minutes

Get a sample configured with your account settings or check it out on Github.

View on Github
System requirements: Android Studio 2.3 | Android SDK 25 | Emulator - Nexus 5X - Android 6.0

This tutorial shows you how to get and modify the user's profile data with Auth0 in your Android apps.

Before You Start

Before you continue with this tutorial, make sure that you have completed the Login and Session Handling tutorials. To call the API applications, you need a valid Access Token and ID Token.

Before launching the login process, you need to make sure the authorization server allows you to read and edit the current user profile. To do that, ask for the openid profile email read:current_user update:current_user_metadata scope and the Management API audience, which happens to include the User Info audience as well. Find the snippet in which you initialize the WebAuthProvider class. To that snippet, add the line withScope("openid profile email offline_access read:current_user update:current_user_metadata") and withAudience(String.format("https://%s/api/v2/", getString(R.string.com_auth0_domain))).

Note that the Management API audience value ends in / in contrast to the User Info audience.

Request User Data

To get the user's information:

  1. Use the user's Access Token to call the userInfo method in the AuthenticationAPIClient application instance. The profile obtained this way is OIDC-conformant. Depending on the scopes you requested when logging in, the profile contains different information. The result will never contain fields outside the OIDC specification.
  2. Get the user's full profile using the Management API. Since fields such as user_metadata are not part of the OIDC specification you need to obtain the full profile to read them. This step is explained next:

Create an instance of the Users API application using the Access Token that you saved in the log in step. This Access Token can be used to authorize requests since you've requested the Management API audience and the corresponding user entity scopes in the login step. In the snippet bellow we obtain the token from the extras that the LoginActivity class has passed when starting this activity. The API application is used to request the user's profile data.

Create now an instance of the Authentication API application. This time is used to call the userinfo endpoint and retrieve the ID of the user to whom the token was issued.

Do not hardcode the Auth0 domain and clientId values when creating the Auth0 instance. We recommend you add them to the strings.xml file.

To get the user's profile:

  1. Use the user's Access Token to call the userInfo method in the AuthenticationAPIClient application instance. You get an instance of the UserProfile profile. The profile is OIDC-conformant. Depending on the on the scopes you requested, the profile contains different information.
  2. To get the user's full profile, use the Management API. Use the user ID obtained in previous step to call getProfile on the Users API application and obtain the full user profile. Use the received data to update the layout.

Access the Data Inside the Received Profile

Default information

At this point, you already have access to the full version of the UserProfile instance. You can use this data wherever you need it.

Some examples are:

Modifying the UI

You cannot modify the UI inside the callback methods as they run in a different thread. To solve this issue you can choose between three options:

  • Persist the data and then retrieve it
  • Create a task in the UI thread and run it. i.e. using the Activity#runOnUiThread method.
  • Create a Handler to post the information

Additional information

You can handle additional information within any of the following maps:

A. User metadata

The userMetadata map contains user profile fields that can be added from the client-side (such as when a user edits their profile). You can access this information in the following way:

You can choose the key names and value types for subscripting the user_metadata map.

B. App metadata

The appMetadata map contains fields that are usually added with a Rule or a Hook. For native platforms, this information is read-only.

To learn more about metadata, see Metadata.

C. Extra information

The extraInfo map contains additional values that are stored in Auth0 but not mapped to a UserProfile getter method. For native platforms, this information is read-only.

Update the User's Profile

You can only update the user metadata. To do this, create a Map<String, Object> object and add the information you want to store.

Update the information with the Users API application created before:

A call to updateMetadata will replace any previous user metadata stored in Auth0. Remember to copy the old values that you wish to maintain.

Use Auth0 for FREE