Migrating from Supabase Auth to Better Auth
In this guide, we'll walk through the steps to migrate a project from Supabase Auth to Better Auth.
This migration will invalidate all active sessions. While this guide doesn't currently cover migrating two-factor (2FA) or Row Level Security (RLS) configurations, both should be possible with additional steps.
Before You Begin
Before starting the migration process, set up Better Auth in your project. Follow the installation guide to get started.
Connect to your database
You'll need to connect to your database to migrate the users and accounts. Copy your DATABASE_URL
from your Supabase project and use it to connect to your database. And for this example, we'll need to install pg
to connect to the database.
And then you can use the following code to connect to your database.
Setup Social Providers (Optional)
Add social providers you have enabled in your Supabase project in your auth config.
Add admin and anonymous plugins (Optional)
Add the admin and anonymous plugins to your auth config.
Run the migration
Run the migration to create the necessary tables in your database.
This will create the following tables in your database:
This tables will be created on the public
schema.
Copy the migration script
Now that we have the necessary tables in our database, we can run the migration script to migrate the users and accounts from supabase to better auth.
Start by creating a .ts
file in your project.
And then copy and paste the following code into the file.
Customize the migration script (Optional)
name
: the migration script will use the user's email as the name. You might want to customize it if you have the user display name in your database.socialProviderList
: the migration script will use the social providers you have enabled in your auth config. You might want to customize it if you have additional social providers that you haven't enabled in your auth config.role
: removerole
if you're not using theadmin
pluginisAnonymous
: removeisAnonymous
if you're not using theanonymous
plugin.- update other tables that reference the
users
table to use theid
field.
Run the migration script
Run the migration script to migrate the users and accounts from supabase to better auth.
Update your code
Update your codebase from supabase auth calls to better auth api.
Here's a list of the supabase auth api calls and their better auth counterparts.
supabase.auth.signUp
->authClient.signUp.email
supabase.auth.signInWithPassword
->authClient.signIn.email
supabase.auth.signInWithOAuth
->authClient.signIn.social
supabase.auth.signInAnonymously
->authClient.signIn.anonymous
supabase.auth.signOut
->authClient.signOut
supabase.auth.getSession
->authClient.getSession
- you can also useauthClient.useSession
for reactive state
Learn more:
- Basic Usage: Learn how to use the auth client to sign up, sign in, and sign out.
- Email and Password: Learn how to add email and password authentication to your project.
- Anonymous: Learn how to add anonymous authentication to your project.
- Admin: Learn how to add admin authentication to your project.
- Email OTP: Learn how to add email OTP authentication to your project.
- Hooks: Learn how to use the hooks to listen for events.
- Next.js: Learn how to use the auth client in a Next.js project.
Middleware
To protect routes with middleware, refer to the Next.js middleware guide or your framework's documentation.
Wrapping Up
Congratulations! You've successfully migrated from Supabase Auth to Better Auth.
Better Auth offers greater flexibility and more features—be sure to explore the documentation to unlock its full potential.