# Migrate from Clerk

> Move Clerk users, password digests, TOTP secrets, social identities and organizations with their roles into Pontive with pontive migrate, then replace Clerk's components and auth() helper with pontkit's.

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

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 Backend API: primary email and whether it is verified, primary phone, username, first and last name, an uploaded image, `created_at`; banned users as deactivated | `unsafe_metadata`: users could write it, so it is not carried over |
| `public_metadata` as `metadata`, `private_metadata` as `private_metadata` | Password digests and TOTP secrets without the dashboard's CSV export: the Backend API never returns them |
| Password digests from the CSV export, for the hashers `bcrypt`, `argon2i`, `argon2id`, `bcrypt_sha256_django`, `pbkdf2_sha256_django`, `scrypt_werkzeug`, `phpass` and `ldap_ssha` | Digests from any other Clerk hasher: those users use **Forgot password** at their first sign-in |
| TOTP secrets from the CSV export | Backup codes, which the CSV does not hold; passkeys |
| Google, GitHub and Apple identities, and custom OIDC providers (`custom_…`) as `oidc` | Microsoft identities and providers Pontive has no portable type for: those users link by verified email |
| Organizations (name, slug, `public_metadata`) and memberships with their role | Organization invitations and domains; users without an email |
| The Clerk user id (`user_…`) as legacy subject, and as `external_id` unless the user already has one | |

**The CSV export.** Export your users as CSV from the Clerk Dashboard. The exporter reads its `id`,
`password_digest`, `password_hasher` and `totp_secret` columns and merges them into the Backend
API's users. Clerk does not document those column names; check that your file has them.

## 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
export CLERK_SECRET_KEY=sk_live_...                # the Clerk instance you are leaving

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

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

```sh
npx pontive@latest migrate export clerk --csv clerk-users.csv   # writes pontive-users.ndjson
npx pontive@latest migrate import pontive-users.ndjson --source msrc_...
```

`--skip-organizations` leaves organizations out; `--external-id none` leaves `external_id` unset
for users who have no Clerk `external_id` of their own. The export retries Clerk's rate limits on
its own.

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

Clerk's organization roles are `org:admin`, `org:member` and your custom `org:…` keys. Map each to an
`org` role in Pontive. Every new organization needs an `owner`, and Clerk has no owner role, so map
its admins to `owner`:

```sh
npx pontive@latest migrate import pontive-users.ndjson --source msrc_... \
  --role-map org:admin=owner,org:member=member \
  --identity-connection google=app_...:google \
  --identity-connection github=app_...:github \
  --totp-app app_...
```

- Create `member` and any custom role on the `org` type first; see
  [Import options](/docs/guides/migrate#import-options). A member holding an unmapped role is left
  out and reported as `role_not_mapped`.
- `--totp-app` names each Pontive app whose sign-in should ask for the imported TOTP code. The app
  must have TOTP on. Without it, TOTP secrets are left out and those users enroll again.

## 5. Apply [#5-apply]

```sh
npx pontive@latest migrate import pontive-users.ndjson --source msrc_... \
  --role-map org:admin=owner,org:member=member \
  --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`. Delete the CSV export
too: it holds the same digests and secrets.

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

pontkit's components map almost one to one onto Clerk's. Follow the
[Next.js quickstart](/docs/quickstart/nextjs) for the provider, the auth proxy and `lib/pontive.ts`,
then replace Clerk's pieces:

```sh
npm uninstall @clerk/nextjs
npm install @pontive/pontkit-nextjs
```

| Clerk | pontkit (`@pontive/pontkit-nextjs`) |
| --- | --- |
| `<ClerkProvider>` | `<Provider domain appId signinUrl signupUrl>`, in a client component |
| `clerkMiddleware()` | `pontive().middleware(req)` in `proxy.ts` |
| `<SignIn />`, `<SignUp />` | `<Signin />`, `<Signup />` |
| `<UserButton />` | `<AccountMenu />` |
| `<UserProfile />` | `<AccountSettings />` |
| `<OrganizationSwitcher />` | `<OrganizationSwitcher />` |
| `<OrganizationProfile />` members tab | `<MemberManagement />` |
| `<SignedIn>`, `<SignedOut>` | `<SignedIn>`, `<SignedOut>` |
| `const { userId, orgId, orgRole } = await auth()` | `const session = await pontive().auth()`: `session.user.id`, `session.organization?.id`, `session.organization?.roles` |
| `has({ permission: "org:invoice:read" })` | `session.organization?.permissions.includes("invoice:read")` |
| `useAuth().getToken()` | `Auth.getCurrentSession()?.access_token`, `Auth` from `@pontive/oidc-client` |

```tsx title="app/dashboard/page.tsx"
import { pontive } from "@/lib/pontive";

export default async function Dashboard() {
  // Was: const { userId, orgId } = await auth();
  const session = await pontive().auth();
  if (!session) return <p>Not signed in.</p>;

  return <p>{session.user.email} in {session.organization?.id ?? "no organization"}</p>;
}
```

Clerk's `org:` prefix on permissions does not carry over: Pontive permissions are plain keys such as
`invoice:read`. Values your app read from `publicMetadata` are on the user's `metadata`, and the
claims template reads them as `{{ user.metadata.<key> }}`.

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

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

- Export again, CSV included, just before switching, and import into the same run with
  `--run uimp_...`.
- If your database keys users by `user_…`, set the app's claims template to
  `{ "legacy_user_id": "{{ user.external_id }}" }`.
- Turn off Clerk's emails and sign-ups once the app is on Pontive. 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, and organizations with other members, are kept and
listed. See [Revert](/docs/guides/migrate#revert).
