# Migrate from Firebase Authentication

> Move Firebase Authentication users and their scrypt password hashes into Pontive with pontive migrate, check the project's hash parameters before importing, then replace firebase/auth with pontkit.

Source: https://www.pontive.com/docs/guides/migrate/firebase

The steps below use the `pontive migrate` CLI. Read [Migrate to Pontive](/docs/guides/migrate) first
for how runs, dry runs and matching work.

## 1. What moves and what does not [#1-what-moves-and-what-does-not]

| Moves | Does not move |
| --- | --- |
| Users from the Admin SDK's `listUsers`: email and whether it is verified, phone number, display name, photo URL, creation time; disabled users as deactivated; custom claims as `private_metadata` | Password hashes, unless the service account may read the project's hash configuration (below). Without them, password users use **Forgot password** at their first sign-in |
| Firebase's modified-scrypt hashes and salts. Their project parameters go on the migration source, never in the file | SMS and TOTP second factors: Firebase exports no secret, so those users enroll again |
| Google, GitHub and Apple identities, and OIDC providers (`oidc.…`) as `oidc` | Microsoft identities and providers Pontive has no portable type for (Facebook, X, …): those users link by verified email |
| The Firebase `uid`, as legacy subject and `external_id` | Phone-only and anonymous users (Pontive requires an email); Identity Platform tenants, which are not organizations. Firebase has no organizations |

## 2. Create the migration source [#2-create-the-migration-source]

The exporter uses a service account through `GOOGLE_APPLICATION_CREDENTIALS`. For `listUsers` to
return hashes, the account also needs `firebaseauth.configs.getHashConfig`, which takes a custom IAM
role; without it every password user comes out without a hash, and the export report says so.

Firebase verifies passwords with four project parameters. In the Firebase console, open
**Authentication**, then **Users**, then the menu above the user list, and **Password hash
parameters**. Save the text it shows to a file and give it to the source:

```sh
export PONTIVE_SECRET_KEY=pontive_sk_test_us_...   # an admin key of your development instance

npx pontive@latest migrate source create --name "Firebase" --type firebase \
  --hash-config firebase-hash.txt                   # prints msrc_...
```

The file holds the signer key: delete it afterwards. Or give the values one by one, with the signer
key in the environment rather than a flag:

```sh
FIREBASE_SIGNER_KEY=... npx pontive@latest migrate source create --name "Firebase" --type firebase \
  --salt-separator Bw== --rounds 8 --mem-cost 14
```

Pontive stores the signer key encrypted and never returns it, only a four-character preview.

## 3. Export, then dry run [#3-export-then-dry-run]

```sh
export GOOGLE_APPLICATION_CREDENTIALS=./service-account.json

npx -p pontive@latest -p firebase-admin pontive migrate export firebase   # writes pontive-users.ndjson
```

`firebase-admin` is not installed with `pontive`, hence `-p`. Before importing, check the source's
parameters against one user whose password you know, such as a test account:

```sh
PONTIVE_SAMPLE_PASSWORD='their password' npx pontive@latest migrate source validate msrc_... \
  --file pontive-users.ndjson --email tester@acme.com
```

`ok` means imported passwords will verify; `FAILED` means a parameter is wrong, and every Firebase
password would fail. Fix it with `migrate source update msrc_... --hash-config <file>`. Then dry-run:

```sh
npx pontive@latest migrate import pontive-users.ndjson --source msrc_...
```

## 4. Map roles and connections [#4-map-roles-and-connections]

Firebase has no organizations or roles to map. Map the social identities to the Pontive app's auth
connections, or leave a type out and its users link by verified email:

```sh
npx pontive@latest migrate import pontive-users.ndjson --source msrc_... \
  --identity-connection google=app_...:google \
  --identity-connection apple=app_...:apple
```

Import an Apple identity only when your Pontive Apple connection uses the same Apple developer team
as Firebase did, and an `oidc` one only for the same issuer: their subjects are per team and per
issuer.

## 5. Apply [#5-apply]

```sh
npx pontive@latest migrate import pontive-users.ndjson --source msrc_... \
  --identity-connection google=app_...:google \
  --identity-connection apple=app_...:apple \
  --apply --delete-after
```

Firebase's scrypt is below OWASP's minimum, so each hash is wrapped in argon2id at import and
re-hashed natively at the user's first sign-in. Keep the migration source until then: Pontive
refuses to delete it, or to remove its parameters, while imported hashes still need them.

## 6. Swap the SDK [#6-swap-the-sdk]

Follow the [React quickstart](/docs/quickstart/react) or the [Next.js quickstart](/docs/quickstart/nextjs),
then replace each Firebase call:

| `firebase/auth` | pontkit |
| --- | --- |
| `signInWithEmailAndPassword`, `signInWithPopup`, FirebaseUI | `<Signin />` from `@pontive/pontkit-react` |
| `createUserWithEmailAndPassword` | `<Signup />` |
| `onAuthStateChanged(auth, …)` | `useAuthState()` (`isAuthenticated`, `resolved`), `useSession()`, or `<SignedIn>` and `<SignedOut>` |
| `auth.currentUser` | `Auth.getAuthenticatedUser()`, `Auth` from `@pontive/oidc-client` |
| `signOut(auth)` | `Auth.signOut()` |
| `user.getIdToken()` | `Auth.getCurrentSession()?.access_token` |
| `firebase-admin`'s `verifyIdToken(token)` | `rs.requireBearer()` from `@pontive/pontkit-server/resource-server` ([Protect an API](/docs/guides/protect-an-api)) |
| `user.uid` | the token's `sub` (`usr_…`) |

**Firestore and Storage security rules** read `request.auth` from Firebase Authentication, which no
longer signs anyone in. Either move those reads and writes behind your server, which verifies the
Pontive token and uses the Admin SDK, or keep the rules by signing the browser in to Firebase with a
custom token your server mints for the Pontive user (`npm install firebase-admin`). With the
claims template carrying the old uid (step 7), rules on `request.auth.uid` keep matching imported
users:

```ts title="app/api/firebase-token/route.ts"
import { initializeApp, getApps } from "firebase-admin/app";
import { getAuth } from "firebase-admin/auth";
import { pontive } from "@/lib/pontive";

if (getApps().length === 0) initializeApp();

export async function POST() {
  const session = await pontive().auth(); // verified by pontkit
  if (!session) return new Response("Unauthorized", { status: 401 });

  // The imported user's Firebase uid, from the claims template; users created in Pontive have none.
  const legacy = session.claims.legacy_user_id;
  const uid = typeof legacy === "string" ? legacy : session.user.id;

  return Response.json({ token: await getAuth().createCustomToken(uid) });
}
```

The browser then calls `signInWithCustomToken(getAuth(), token)` from `firebase/auth`.

## 7. Cut over [#7-cut-over]

Follow the [cutover checklist](/docs/guides/migrate#cut-over). For Firebase in particular:

- Export again just before switching and import into the same run with `--run uimp_...`.
- Set the app's claims template to `{ "legacy_user_id": "{{ user.external_id }}" }` if your data or
  rules key users by Firebase uid.
- Turn off sign-ups and Firebase's email templates once the app is on Pontive. Users sign in once
  more.
- `npx pontive@latest migrate source get msrc_...` shows how many imported passwords are still
  waiting for their user's first sign-in.

## 8. Revert [#8-revert]

```sh
npx pontive@latest migrate revert uimp_...          # dry run
npx pontive@latest migrate revert uimp_... --apply
```

Users who have signed in to Pontive since are kept and listed. See
[Revert](/docs/guides/migrate#revert).
