> ## Documentation Index
> Fetch the complete documentation index at: https://auth0-docs-event-stream-action-templates.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> How to use Auth0.Android to manage users

# Auth0.Android: User Management

export const AuthCodeBlock = ({filename, icon, language, highlight, children}) => {
  const [displayText, setDisplayText] = useState(children);
  const [copyText, setCopyText] = useState(children);
  const wrapperRef = React.useRef(null);
  useEffect(() => {
    let unsubscribe = null;
    function init() {
      if (!window.autorun || !window.rootStore) {
        return;
      }
      unsubscribe = window.autorun(() => {
        let processedChildrenForDisplay = children;
        let processedChildrenForCopy = children;
        for (const [key, value] of window.rootStore.variableStore.values.entries()) {
          const escapedKey = key.replaceAll(/[.*+?^${}()|[\]\\]/g, (String.raw)`\$&`);
          let displayValue = value;
          if (key === "{yourClientSecret}" && value !== "{yourClientSecret}") {
            displayValue = value.substring(0, 3) + "*****MASKED*****";
          }
          processedChildrenForDisplay = processedChildrenForDisplay.replaceAll(new RegExp(escapedKey, "g"), displayValue);
          processedChildrenForCopy = processedChildrenForCopy.replaceAll(new RegExp(escapedKey, "g"), value);
        }
        setDisplayText(processedChildrenForDisplay);
        setCopyText(processedChildrenForCopy);
      });
    }
    if (window.rootStore) {
      init();
    } else {
      window.addEventListener("adu:storeReady", init);
    }
    return () => {
      window.removeEventListener("adu:storeReady", init);
      unsubscribe?.();
    };
  }, [children]);
  useEffect(() => {
    if (!wrapperRef.current) return;
    const originalWriteText = navigator.clipboard.writeText.bind(navigator.clipboard);
    let isOverriding = false;
    const handleClick = e => {
      const button = e.target.closest('[data-testid="copy-code-button"]');
      if (!button || !wrapperRef.current.contains(button)) return;
      isOverriding = true;
      navigator.clipboard.writeText = text => {
        if (isOverriding) {
          isOverriding = false;
          navigator.clipboard.writeText = originalWriteText;
          return originalWriteText(copyText);
        }
        return originalWriteText(text);
      };
      setTimeout(() => {
        if (isOverriding) {
          isOverriding = false;
          navigator.clipboard.writeText = originalWriteText;
        }
      }, 100);
    };
    const wrapper = wrapperRef.current;
    wrapper.addEventListener('click', handleClick, true);
    return () => {
      wrapper.removeEventListener('click', handleClick, true);
      if (navigator.clipboard.writeText !== originalWriteText) {
        navigator.clipboard.writeText = originalWriteText;
      }
    };
  }, [copyText]);
  return <div ref={wrapperRef}>
      <CodeBlock filename={filename} icon={icon} language={language} lines highlight={highlight}>
        {displayText}
      </CodeBlock>
    </div>;
};

The <Tooltip tip="Management API: A product to allow customers to perform administrative tasks." cta="View Glossary" href="/docs/glossary?term=Management+API">Management API</Tooltip> provides functionality that you can use to manage users of your application, including these types of tasks:

* Link separate user accounts from different providers, tying them to a single profile. To learn more, read [User Account Linking](/docs/manage-users/user-accounts/user-account-linking).
* Unlink user accounts, returning them to separate identities.
* Update user metadata. To learn more, read [Understand How Metadata Works in User Profiles](/docs/manage-users/user-accounts/metadata).

## 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.

export const codeExample = `val account = Auth0("{yourClientId}", "{yourDomain}")
val client = UsersAPIClient(account, "token")`;

<AuthCodeBlock children={codeExample} language="kotlin" />

## Link users

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.

```kotlin lines theme={null}
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!
        }
    })
```

## Unlink users

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).

```kotlin lines theme={null}
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.

```kotlin lines theme={null}
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!
        }
    })
```
