# Migrate from Supabase Auth

> Move Supabase Auth users, bcrypt password hashes, TOTP factors and social identities into Pontive with pontive migrate, keep your data in Supabase, and keep Row Level Security policies working through the cutover.

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

Your database, storage and functions stay in Supabase; only sign-in moves. Supabase then trusts
Pontive's tokens as a third-party issuer, as [Use Pontive with Supabase](/docs/guides/supabase)
sets up. 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 `auth.users`, except deleted and anonymous ones: email and whether it is confirmed, phone and whether it is confirmed, `full_name` or `name`, `avatar_url` or `picture`, `created_at`; banned users as deactivated | Your own tables: the exporter never reads them, and they stay where they are |
| `raw_user_meta_data` as `metadata`, `raw_app_meta_data` (without `provider` and `providers`) as `private_metadata` | Passwords and TOTP secrets encrypted at rest by Supabase: those users use **Forgot password** and enroll TOTP again |
| bcrypt password hashes from `encrypted_password` (and argon2 ones, if you imported any) | MFA recovery codes, which Supabase stores as hashes; phone and WebAuthn factors |
| Verified TOTP factors from `auth.mfa_factors` | `azure` (Entra ID) identities, SSO identities and providers Pontive has no portable type for: those users link by verified email |
| Google, GitHub and Apple identities from `auth.identities` | Users without an email; organizations, which Supabase does not have |
| The Supabase user id (a UUID), as legacy subject and `external_id` | |

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

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

npx pontive@latest migrate source create --name "Supabase" --type supabase   # prints msrc_...
```

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

The exporter reads the `auth` schema over Postgres, so it needs the project's connection string
(the **Connect** button in the Supabase dashboard) as the `postgres` user. Put it in the environment:
a `--db-url` flag would land in your shell history.

```sh
export SUPABASE_DB_URL='postgresql://postgres:...@...:5432/postgres'

npx -p pontive@latest -p pg pontive migrate export supabase   # writes pontive-users.ndjson
npx pontive@latest migrate import pontive-users.ndjson --source msrc_...
```

`pg` is not installed with `pontive`, hence `-p`. `--external-id none` leaves `external_id` unset,
which you do not want here: step 6 uses it.

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

Supabase has no organizations or roles to map. Map the social identities to the Pontive app's auth
connections, and name the apps that should ask for imported TOTP codes:

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

Leave a connection type out and its users link by verified email. Leave `--totp-app` out and TOTP
secrets are dropped, and those users enroll again; the app named must have TOTP on.

## 5. Apply [#5-apply]

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

Failed rows: `npx pontive@latest migrate records uimp_... --outcome failed`.

## 6. Swap the SDK, keep your policies [#6-swap-the-sdk-keep-your-policies]

Set up Pontive as Supabase's third-party issuer and pass the Pontive token to supabase-js, as in
[Use Pontive with Supabase](/docs/guides/supabase), steps 1 to 3. Then replace the `supabase.auth`
calls, which throw in a client created with `accessToken`:

| Supabase Auth | pontkit |
| --- | --- |
| `signInWithPassword`, `signInWithOAuth`, `signInWithOtp`, the Auth UI | `<Signin />` from `@pontive/pontkit-react` |
| `signUp` | `<Signup />` |
| `getSession()`, `getUser()` in the browser | `Auth.getCurrentSession()`, `Auth.getAuthenticatedUser()`, `Auth` from `@pontive/oidc-client` |
| `onAuthStateChange` | `useSession()` and `useAuthState()` from `@pontive/pontkit-react` |
| `signOut()` | `Auth.signOut()` |
| `@supabase/ssr`'s server client and `getUser()` | `pontive().auth()` from `@pontive/pontkit-nextjs/server`, then a client with `accessToken: async () => session?.accessToken ?? null` |

**Row Level Security.** `auth.uid()` casts the token's `sub` to a UUID, and a Pontive `sub`
(`usr_…`) is not one, so every policy written with `auth.uid()` fails. Carry the old id in the token
and compare with it. Set the app's claims template to:

```json
{
  "role": "authenticated",
  "supabase_uid": "{{ user.external_id }}"
}
```

and rewrite each policy:

```sql
-- Was: using (user_id = auth.uid())
alter policy "Users read their own notes" on public.notes
  using (user_id = ((select auth.jwt()->>'supabase_uid')::uuid));
```

This keeps your UUID columns and foreign keys as they are for imported users. Users who sign up in
Pontive have no `external_id`, so the claim is absent and those policies match nothing for them.
For them, and as the lasting fix, key per-user rows by the token's `sub` in a `text` column. The
run's records map each Supabase id to its Pontive user:

```sh
npx pontive@latest migrate records uimp_... --kind user --json \
  | jq -r '.records[] | select(.user_id) | [.key, .user_id] | @csv' > user-ids.csv
```

Load that into a table and update your columns from it. Tables that reference `auth.users` keep
working while the rows there exist: do not delete users from Supabase Auth while a foreign key with
`on delete cascade` points at them.

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

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

- Export again just before switching and import into the same run with `--run uimp_...`.
- Turn off new sign-ups in Supabase Auth, which cannot itself be turned off, and add the restrictive
  issuer policy from [Use Pontive with Supabase](/docs/guides/supabase#accept-only-your-apps-tokens)
  so its tokens never reach your data.
- Users sign in once more.

## 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. Nothing in Supabase changes. See
[Revert](/docs/guides/migrate#revert).
