Adapted Firebase Auth Migration for the FlutterFlow project
Published: 2023-01-02 20:01:08

Huge thanks to the FlutterFlow team for providing this great feature. Supabase promises very bright opportunities and a future for developers as an open-source project. I know it's going to be difficult, but I think now is a good time to start migrating the whole Foodshare Firebase to Supabase piece by piece :)

Supabase provides several tools to help migrate auth users from a Firebase project to a Supabase project. There are two parts to the migration process:

- firestoreusers2json (TypeScript, JavaScript) exports users from an existing Firebase project to a .json file on your local system.

- import_users (TypeScript, JavaScript) imports users from a saved .json file into your Supabase project (inserting those users into the auth.users table of your PostgreSQL database instance).

**Set up the migration tool**

Before we start we need to Google auth in terminal:
`gcloud auth login`

Set your project:
`gcloud config set project <your-project>`

Install npm moment module to avoid an error with it:
`npm i moment`

1. Clone the firebase-to-supabase repository:
`git clone https://github.com/supabase-community/firebase-to-supabase.git`

CD to /auth directory and in the /auth directory, create a file named supabase-service.json with the following contents:

`{
  "host": "database.server.com",
  "password": "secretpassword",
  "user": "postgres",
  "database": "postgres",
  "port": 5432
}`

2. Go to the Database settings for your project in the Supabase Dashboard.

3. Under Connection Info, copy the Host string and replace the entry in your supabase-service.json file.

4. Enter the password you used when you created your Supabase project in the password entry in the supabase-service.json file.

**Generate a Firebase private key**

1. Log in to your Firebase Console and open your project.
2. Click the gear icon next to Project Overview in the sidebar and select Project Settings.
3. Click Service Accounts and select Firebase Admin SDK.
4. Click Generate new private key.
5. Rename the downloaded file to firebase-service.json.

**Save your Firebase password hash parameters**

1. Log in to your Firebase Console and open your project.
2. Select Authentication (Build section) in the sidebar.
3. Select Users in the top menu.
4. At the top right of the users list, open the menu (3 dots) and click Password hash parameters.
5. Copy and save the parameters for base64_signer_key, base64_salt_separator, rounds, and mem_cost.

`hash_config {
  algorithm: SCRYPT,
  base64_signer_key: XXXX/XXX+XXXXXXXXXXXXXXXXX+XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX==,
  base64_salt_separator: Aa==,
  rounds: 8,
  mem_cost: 14,
}`

**Command line options**

Dump Firestore users to a JSON file #

`node firestoreusers2json.js [<filename.json>] [<batch_size>]`

In my case, it was: `node firestoreusers2json.js users.json 1000` 

filename.json: (optional) output filename (defaults to ./users.json)
batchSize: (optional) number of users to fetch in each batch (defaults to 100)
If an error like the one below occurs, check your JSON dump file for a missing comma. This is easily detected by the highlighted line in VS Code. 
``node:events:491`
      throw er; // Unhandled 'error' event check out your dumped file`

Import JSON users file to Supabase Auth (PostgreSQL: auth.users) 

`node import_users.js <path_to_json_file> [<batch_size>]`

In my case: `node import_users.js ./users.json 1000`

- path_to_json_file: full local path and filename of .json input file (of users)
- batch_size: (optional) number of users to process in a batch (defaults to 100)

**Notes**
For more advanced migrations, including the use of a middleware server component for verifying a user's existing Firebase password and updating that password in your Supabase project the first time a user logs in, see the firebase-to-supabase repo.

Thank you :)