Auth0.Android: User Management

The Management API provides functionality that you can use to manage users of your application, including these types of tasks:

Initialize the UsersAPIClient

To get started, create a new UsersAPIClient instance by passing it the account and the token for the primary identity. In the case of linking users, this primary identity is the user profile that you want to "keep" the data for, and to which you plan to link other identities.

val account = Auth0("YOUR_CLIENT_ID", "YOUR_DOMAIN")
val client = UsersAPIClient(account, "token")

Linking user accounts lets a user authenticate from any of their accounts. No matter which account they authenticate with, they log in with the same profile. Without account linking, Auth0 treats every different account as a separate profile.

The link method accepts two parameters: the primary user id and the secondary user token (the token obtained after login with this identity). The user id in question is the unique identifier for this user account. If the id is in the format facebook|1234567890, the id required is the portion after the delimiting pipe.

client
    .link("primary user id", "secondary user token")
    .start(object: Callback<List<UserIdentity>, ManagementException>() {
        override fun onSuccess(payload: List<UserIdentity>) {
            // Got the updated identities! Accounts linked.
        }

        override fun onFailure(error: ManagementException) {
            // Error!
        }
    })

Unlinking users returns the accounts to separate profiles. The unlink method takes three parameters: the primary user id, the secondary user id, and the secondary provider (of the secondary user).

users
    .unlink("primary user id", "secondary user id", "secondary provider")
    .start(object: Callback<List<UserIdentity>, ManagementException>() {
        override fun onSuccess(payload: List<UserIdentity>) {
            // Got the updated identities! Accounts linked.
        }

        override fun onFailure(error: ManagementException) {
            // Error!
        }
    })

When accounts are linked, the secondary account's metadata does not merge with the primary account's metadata. Similarly, when unlinking two accounts, the secondary account does not retain the primary account's metadata.

Updating user metadata

When updating user metadata, you create a metadata object and then call the updateMetadata method, passing it the user id and the metadata object. The values in this object overwrite existing values with the same key, or add new values for those that don't yet exist in the user metadata.

val metadata = mutableMapOf<String, Any?>()
metadata.put("name", listOf("My", "Name", "Is"));
metadata.put("phoneNumber", "1234567890");

users
    .updateMetadata("user id", metadata)
    .start(object: Callback<UserProfile, ManagementException>() {
        override fun onSuccess(payload: UserProfile) {
            // Metadata updated
        }

        override fun onFailure(error: ManagementException) {
            // Error!
        }
    })