Migrating from Auth0 to Better Auth
In this guide, we'll walk through the steps to migrate a project from Auth0 to Better Auth — including email/password with proper hashing, social/external accounts, two-factor authentication, and more.
This migration will invalidate all active sessions. This guide doesn't currently show you how to migrate Organizations but it should be possible with additional steps and the Organization Plugin.
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. You can use any database you want, but for this example, we'll use PostgreSQL.
And then you can use the following code to connect to your database.
Enable Email and Password (Optional)
Enable the email and password in your auth config and implement your own logic for sending verification emails, reset password emails, etc.
See Email and Password for more configuration options.
Setup Social Providers (Optional)
Add social providers you have enabled in your Auth0 project in your auth config.
Add Plugins (Optional)
You can add the following plugins to your auth config based on your needs.
Admin Plugin will allow you to manage users, user impersonations and app level roles and permissions.
Two Factor Plugin will allow you to add two-factor authentication to your application.
Username Plugin will allow you to add username authentication to your application.
Generate Schema
If you're using a custom database adapter, generate the schema:
or if you're using the default adapter, you can use the following command:
Create the migration script
Create a new file called migrate-auth0.ts
in the scripts
folder and add the following code:
Instead of using the Management API, you can use Auth0's bulk user export functionality and pass the exported JSON data directly to the auth0Users
array. This is especially useful if you need to migrate password hashes and complete user data, which are not available through the Management API.
Important Notes:
- Password hashes export is only available for Auth0 Enterprise users
- Free plan users cannot export password hashes and will need to request a support ticket
- For detailed information about bulk user exports, see the Auth0 Bulk User Export Documentation
- For password hash export details, refer to Exporting Password Hashes
Example:
Make sure to replace the Auth0 environment variables with your own values:
AUTH0_DOMAIN
AUTH0_CLIENT_ID
AUTH0_SECRET
Run the migration
Run the migration script:
Important considerations:
- Test the migration in a development environment first
- Monitor the migration process for any errors
- Verify the migrated data in Better Auth before proceeding
- Keep Auth0 installed and configured until the migration is complete
- The script handles bcrypt password hashes by default. For custom password hashing algorithms, you'll need to modify the
migratePassword
function
Verify the migration
After running the migration, verify that:
- All users have been properly migrated
- Social connections are working
- Password-based authentication is working
- Two-factor authentication settings are preserved (if enabled)
- User roles and permissions are correctly mapped
Update your components
Now that the data is migrated, update your components to use Better Auth. Here's an example for the sign-in component:
Remove Auth0 Dependencies
Once you've verified everything is working correctly with Better Auth, remove Auth0:
Additional Considerations
Password Migration
The migration script handles bcrypt password hashes by default. If you're using custom password hashing algorithms in Auth0, you'll need to modify the migratePassword
function in the migration script to handle your specific case.
Role Mapping
The script includes a basic role mapping function (mapAuth0RoleToBetterAuthRole
). Customize this function based on your Auth0 roles and Better Auth role requirements.
Rate Limiting
The migration script includes pagination to handle large numbers of users. Adjust the perPage
value based on your needs and Auth0's rate limits.
Wrapping Up
Now! You've successfully migrated from Auth0 to Better Auth.
Better Auth offers greater flexibility and more features—be sure to explore the documentation to unlock its full potential.