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
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.Add optional plugins
Install Better Auth plugins that match Clerk features you were using:
| Clerk feature | Better Auth plugin |
|---|---|
| User management UI | Admin |
| Two-factor authentication | Two Factor |
| Phone number auth | Phone Number |
| Username sign-in | Username |
| Organizations | Organization |
auth.ts
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.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
Update your components
Replace Clerk’s React components with Better Auth equivalents:
components/sign-in.tsx
Update the middleware
Replace
clerkMiddleware with Better Auth’s lightweight cookie check:middleware.ts