Configure Automatic Migration from Your Database
After you create a database connection in the Dashboard, you enable user migration from that database and create custom scripts to determine how the migration happens.
These custom scripts are Node.js code that run in the tenant's sandbox. Auth0 provides templates for most common databases, such as: ASP.NET Membership Provider, MongoDB, MySQL, Oracle, PostgreSQL, SQLServer, Windows Azure SQL Database, and for a web service accessed by Basic Auth.
Navigate to the Connections > Database page in the Auth0 Dashboard, and click Create DB Connection.
Click the Custom Database tab, and enable the Use my own database option.
Click the Settings tab, and enable the Import Users to Auth0 option.
Configure scripts
Click the Custom Database tab, and locate Database Action Scripts to see the scripts you need to configure.
Login: Executes each time a user that is not found in the Auth0 database attempts to log in. Verifies that the user exists in the legacy database without re-prompting the user for their password.
Get User: Executes following any of these actions:
user attempts to sign-up.
user clicks on valid password change confirmation link.
Management API receives a call to the Update a User's Email or Username endpoint.
If an un-migrated user confirms a password change, their user profile will be created in Auth0 with the new password. This user profile will contain all the information returned in the Get User script. All subsequent logins of this user will be performed in Auth0 directly. You may see unexpected behavior if you return differing user profiles in the login
and get_user
scripts.
Verify migration
After you've enabled migration, you can verify the users that have migrated by doing one or both of the following tasks:
Use the List or search users Management API endpoint.
Navigate to the Users page in the Auth0 Dashboard, and review the list of users.
Convert database
Once all your users are in the Auth0 database, you are ready to convert the database so that it uses only users stored in Auth0.
Go to your custom database connection on the Dashboard.
Update the Login Database Action Script to the following:
function login (email, password, callback) {
return callback(null, null);
}
Update the Get User Database Action Script to the following:
function getByEmail (email, callback) {
return callback(null, null);
}
By doing this, you are changing the Login and Get User Database Action Scripts to NO-OP functions. essentially making it behave as a non-custom database connection.
Disconnect legacy database
After you have verified the migration, you can disconnect your legacy database (not the Auth0 database). If you modified the scripts as instructed above, Auth0 will not try to connect to your legacy database.
Keep Import Users to Auth0 (on the Settings page) enabled. This, combined with the NO-OP script changes, will ensure that only the Auth0 users database is used.
Configure rules to execute other functions when a user authenticates to your application.
Troubleshoot migration errors
The most common error message that you may encounter when importing users from a legacy database to an Auth0 custom database is "The user already exists."
When a user is imported, a partial user state is first created on the Auth0 end to make this migration possible. If you delete this user from the Auth0 connection then later recreate the user, you may receive this error. In addition, the Get User script is called on user creation. If you attempt to create a new user in the Auth0 custom database connection and the user already exists in your external database, you will receive this error.
If you encounter a "user already exists" error message, use the Management API's endpoint to delete a connection user to delete the user. Confirm that the user does not exist in your legacy database, then recreate the user.