Android Linking Accounts

This tutorial shows you how to link two different accounts for the same user using Auth0.

Sample Project

Download a sample project specific to this tutorial configured with your Auth0 API Keys.

System Requirements
  • Android Studio 2.3
  • Android SDK 25
  • Emulator - Nexus 5X - Android 6.0
Show requirements

Before You Start

Before you continue with this tutorial, make sure that you have completed the previous tutorials. This tutorial assumes that:

  • You have integrated Auth0 as a dependency in your project.
  • You are familiar with the WebAuthProvider class. To learn more, see the Login and the Session Handling tutorials.
  • You are familiar with the concepts of userId and idToken. You can find info about them in the Session Handling and the User Profile tutorial.

We recommend that you read the Linking Accounts documentation to understand the process of linking accounts.

Enter Account Credentials

Your users may want to link their other accounts to the account they are logged in to.

To achieve this, you need to store the user id for the logged user in the Intent so they can be accessed in other activities.

// app/src/main/java/com/auth0/samples/activities/MainActivity.java
Intent intent = new Intent(MainActivity.this, LoginActivity.class);        
intent.putExtra(Constants.LINK_ACCOUNTS, true);
intent.putExtra(Constants.PRIMARY_USER_ID, profile.getId());
startActivity(intent);

Obtain the stored values in LoginActivity:

// app/src/main/java/com/auth0/samples/activities/LoginActivity.java

boolean linkSessions = getIntent().getExtras().getBoolean(Constants.LINK_ACCOUNTS, false);
String userId = getIntent().getExtras().getString(Constants.PRIMARY_USER_ID);

In the login response, based on the boolean flag set in the first step, decide if you need to show the MainActivity screen, or continue to link the accounts.

// app/src/main/java/com/auth0/samples/activities/LoginActivity.java
@Override
public void onAuthentication(Credentials credentials) {
  if (linkSessions) {
    // Link the accounts
    performLink(credentials.getIdToken())
  } else {
    CredentialsManager.saveCredentials(LoginActivity.this, credentials);
    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
    startActivity(intent);
  }
}

Now, you can link the accounts. To do this, you need the logged-in user's ID and the ID tokens for the two accounts:

  • The saved account the user initially logged in to
  • The second account received in the last login response
// app/src/main/java/com/auth0/samples/activities/LoginActivity.java

private void performLink(String secondaryIdToken) {
  String primaryIdToken = CredentialsManager.getCredentials(LoginActivity.this).getIdToken();
  UsersAPIClient client = new UsersAPIClient(auth0, primaryIdToken);
  client.link(primaryUserId, secondaryIdToken)
          .start(new BaseCallback<List<UserIdentity>, ManagementException>() {
              @Override
              public void onSuccess(List<UserIdentity> payload) {
                  //Accounts linked
              }

              @Override
              public void onFailure(ManagementException error) {
                  //show error
              }
          });

Retrieve the Linked Accounts

To obtain the user's full profile, use the user's ID to call the getProfile method in the UsersAPIClient class. The profile includes the linked accounts as the UserIdentities array.

// app/src/main/java/com/auth0/samples/activities/MainActivity.java

usersClient.getProfile(userInfo.getId())
    .start(new BaseCallback<UserProfile, ManagementException>() {
        @Override
        public void onSuccess(UserProfile fullProfile) {
            //refreshScreenInformation
        }

        @Override
        public void onFailure(ManagementException error) {
            //show error
        }
    });

For more information, check the UserIdentity.java class documentation.

To unlink the accounts, you need to specify the following:

  • user ID for the main account
  • user ID for the linked acocunt
  • the provider name

To instantiate the UsersAPIClient client, use the ID token for the main account.

// app/src/main/java/com/auth0/samples/activities/MainActivity.java

String primaryIdToken = CredentialsManager.getCredentials(LoginActivity.this).getIdToken();
UsersAPIClient client = new UsersAPIClient(auth0, primaryIdToken);

client.unlink(userProfile.getId(), secondaryAccountIdentity.getId(), secondaryAccountIdentity.getProvider())
        .start(new BaseCallback<List<UserIdentity>, ManagementException>() {
            @Override
            public void onSuccess(List<UserIdentity> payload) {
                //Accounts unlinked
            }

            @Override
            public void onFailure(final ManagementException error) {
                //Accounts unlink failed
            }
        });
Previous Tutorial
4. User Profile
Next Tutorial
6. Calling APIs
Use Auth0 for FREECreate free Account