Skip to main content
This guide walks through migrating a project from Clerk to Better Auth, including email/password with correct hashing, social accounts, two-factor data, and middleware.
This migration invalidates all active sessions. Users will need to sign in again after the migration is complete.

1

Set up Better Auth

Follow the installation guide to add Better Auth to your project and connect it to your database. The example below uses PostgreSQL:
auth.ts
2

Configure email and password

Clerk uses bcrypt to hash passwords. Better Auth defaults to scrypt. To let migrated users sign in with their existing passwords, configure Better Auth to use bcrypt:
auth.ts
After all users have logged in at least once post-migration you can switch back to the default scrypt hasher and remove the bcrypt override.
3

Configure social providers

Add the same OAuth providers you had enabled in Clerk:
auth.ts
4

Add optional plugins

Install Better Auth plugins that match Clerk features you were using:
auth.ts
5

Generate the database schema

6

Export users from Clerk

Go to the Clerk dashboard and export your users as a CSV file. Save it as exported_users.csv in your project root. See Clerk’s export docs for step-by-step instructions.
7

Create the migration script

Create scripts/migrate-clerk.ts and add the following script. It reads the exported CSV, fetches full user data from the Clerk API, and inserts everything into your Better Auth database.
scripts/migrate-clerk.ts
8

Run the migration

Always test the migration against a development or staging database first. Monitor the output for errors and verify the migrated data before switching production traffic to Better Auth.
9

Update your components

Replace Clerk’s React components with Better Auth equivalents:
components/sign-in.tsx
10

Update the middleware

Replace clerkMiddleware with Better Auth’s lightweight cookie check:
middleware.ts
11

Remove Clerk

Once you have verified everything works:

Additional resources