Change a Database User's user_id without Reimporting the Password Hash

Overview

It is not possible to change the user_id of an existing user. There are 2 options to address this:

  1. The user profile including the password hash should be exported and then re-imported using the desired user_id, to be able to have a DB user with a different user_id and keep the same password.
  2. If option 1 is not possible, to avoid having to export and re-import all the users, the workaround is to create a new user in a separate database with the new user_id, and then link the identities.

This article explains how to implement option 2.

Applies To

  • Auth0 Database
  • user_id
  • Account Linking

Cause

It may be required to change the user_id of a database user given their custom use case.

Solution

Two separate databases are needed for this workaround to work (DB1 - the original one, with the Auth0 formatted user_id, and DB2, the one in which the user with the custom user_id will be created)

A new user has to be created in DB2 with a random password, the same email as the original user, and the custom user_id, for example:

POST https://<TENANT_DOMAIN>/api/v2/users

 

{

"user_id":"<CUSTOM_ID>",

"user_metadata": {},

"blocked": false,

"app_metadata": {},

"given_name": "<NAME>",

"family_name": "<LASTNAME>",

"name": "<FULL_NAME>",

"connection": "<DB2>",

"email": "<EMAIL>",

"email_verified": true,

"password":"RandomPassword123&"

}

The 2 users have to be linked, selecting the newly created user (the one in DB2) as primary.

POST https://<TENANT_DOMAIN>/api/v2/users/<CUSTOM_ID>/identities



{

"provider": "auth0",

"connection_id": "<DB1_ID>",

"user_id": "<ORIGINAL_AUTH0_USER_ID>"

}

Then, the user will be able to log in using the same (original) credentials, but the user ID received will be the custom one. The client ID used to log in has only the original DB (DB1) enabled.

Using separate databases allows to have 2 different users with the same email. This is needed for the password reset flow to work as expected.