DEV Community

Cover image for Firebase: Migrate users from one project to another using Firebase CLI
Aman Kumar
Aman Kumar

Posted on

Firebase: Migrate users from one project to another using Firebase CLI

Firebase is a popular service used by developers for various backend functionalites which provides a user freindly interface to manage your projects and one them is authentication.

Firebase authentication is very populary service which provides a bunch of authentication methods like email/password, google, facebook, twitter, phone, etc.

Using this we can manage users in our project and provide a seamless authentication experience. But in some cases you might need to move users from one project to another project, like, when you are merging a smaller project into a larger one to keep track of all your mini-projects in one place.

Firebase does not provide any way to migrate users from one project to another but we can do so using the Firebase CLI, which is very easy to follow with a very few steps.

Environment setup

  1. Install Firebase CLI Tools

Run command in command prompt to install firebase-tools globally:

npm install -g firebase-tools

  1. Login to Firebase via CLI

To login to your firebase account via the command line, run command:

firebase login

This will redirect you to Firebase CLI login website. Complete the login process using your Firebase account where you have access to your projects.

Export users from older project

  1. Find the Project ID of the older project

You can find the Project ID of your project though Firebase console > Project Settings > General.

OR

Find a list of your project using CLI using command:

firebase projects:list

Find your older project name and its respective Project ID from the results.

  1. Use CLI to export users

Run the following command to export user from your older project:

firebase auth:export firebase-users.json --format=json --project <YOUR_PROJECT_ID>

  • firebase auth:export: It tells Firebase you're trying to export users.
  • firebase-users.json: File name of the file where you want to export users.
  • --format=json: Specifies the file format of the destination file.
  • --project <YOUR_PROJECT_ID>: It tells Firebase which project you are exporting the users from. Replace with the Project ID of your older project.

Retrieving the Password hash parameters

To successfully import the user into your new project you need some secret keys and values used in your older project.

Go to Authentication > Users > Three dots beside Add User button > Password hash parameters.

This will open a popup with secret keys and their values.

Make note of all the values:

hash_config {
  algorithm: <HASH_ALGO>,
  base64_signer_key: <YOUT_SIGHER_KEY>,
  base64_salt_separator: <YOUR_SALT_SEPARATOR>,
  rounds: <ROUNDS>,
  mem_cost: <MEM_COST>,
}
Enter fullscreen mode Exit fullscreen mode

Import users into your new project

To import users from the file firebase-users.json file to your new project, run the following command:

firebase auth:import firebase-users.json --project=<NEW_PROJECT_ID> --hash-algo=<HASH_ALGO> --hash-key=<YOUR_SIGHER_KEY> --salt-separator=<YOUR_SALT_SEPARATOR> --rounds=<ROUNDS> --mem-cost=<MEM_COST>

Enter fullscreen mode Exit fullscreen mode
  • firebase auth:import: It tells Firebase you'are trying to iport users.
  • firebase-users.json: Path to File with filename to the file where you have exported user data.
  • --project=<NEW_PROJECT_ID>: Project ID of your new project, replace with .
  • <HASH_ALGO>, <YOUR_SIGHER_KEY>, <YOUR_SALT_SEPARATOR>, <ROUNDS>, <MEM_COST>: Refer to the values retrieved from your older project.

After running this command, refresh your users in Firebase console. Now you can see all the users from older project in your new project.

Top comments (0)