Firebase Auth: Edit UID

Building on the answer by RoccoB, the below is a complete set of instructions for changing a user's UID:

  1. Create a new folder, and run npm init with default values.
  2. Run npm install firebase-admin.
  3. Create a NodeJS script file (eg. UpdateUserUID.js), with this code:
let admin = require("firebase-admin");

// config
let email = "XXX";
let serviceAccountData = require("XXX.json");
let adminConfig = {
    credential: admin.credential.cert(serviceAccountData),
    databaseURL: "https://XXX.firebaseio.com",
};
let newUserOverrides = {
    uid: "XXX",
};

Start();
async function Start() {
    console.log("Initializing firebase. databaseURL:", adminConfig.databaseURL);
    admin.initializeApp(adminConfig);

    console.log("Starting update for user with email:", email);
    let oldUser = await admin.auth().getUserByEmail(email);
    console.log("Old user found:", oldUser);

    await admin.auth().deleteUser(oldUser.uid);
    console.log("Old user deleted.");

    let dataToTransfer_keys = ["disabled", "displayName", "email", "emailVerified", "phoneNumber", "photoURL", "uid"];
    let newUserData = {};
    for (let key of dataToTransfer_keys) {
        newUserData[key] = oldUser[key];
    }
    Object.assign(newUserData, newUserOverrides);
    console.log("New user data ready: ", newUserData);

    let newUser = await admin.auth().createUser(newUserData);
    console.log("New user created: ", newUser);
}
  1. Replace email and adminConfig.databaseURL with the correct values.
  2. Replace newUserOverrides.uid with the desired new uid. (you can change some other fields too)
  3. Generate/download a private key for your project's Firebase Admin service account: https://firebase.google.com/docs/admin/setup (can skip to the "Initialize the SDK" section)
  4. Update the serviceAccountData variable's import to point to the key json-file from the previous step.
  5. Run node ./UpdateUserUID.js.
  6. If applicable (I didn't seem to need it), use the "reset password" option in the Firebase Admin Console, to have a password-reset email sent to the user, apparently completing the account update. (Perhaps I didn't need this step since I don't use the accounts/authentications for anything besides sign-in on my website...)

TL;DR: If you need to specify the UID, you'll need to create a new user with that UID.

You can't directly change the UID, but I was able to hack something together using the firebase admin API (docs)

My use case was that I needed to change a user's email address. I tried update email with "Update a User", but this actually ended up changing the UID under the hood. In my app, the UID is tied to so much stuff, that I'd have to do a huge architecture change, so this wasn't an option.

The general way I did this with the API was:

  • Pull Down a user using admin.auth().getUserByEmail
  • Delete the user with admin.auth().deleteUser
  • Create a new user with admin.auth().createUser, using relevant data from the getUserByEmail call above, replacing the email address with the new email.
  • "reset password" in the firebase admin console (I think there's a way to do this programmatically too)
  • User gets an email to reset their password and they have a new account with their old UID.

Unlike admin.auth().updateUser, createUser actually lets you specify a UID.