Skip to main content
The Login script implements the function executed each time a user is required to authenticate. We recommend naming this function login. This script is required for both legacy authentication and for automatic migration. If automatic migration is configured for the connection, the migration process triggers after the first time the user logs in successfully. Auth0 recommends you set permanent user_id on the returned user profile to avoid creating duplicate users.

Login function

The login function should:
  • Send the provided user credentials to the external database’s API.
  • Return the profile data of the user if authentication is successful.
  • Return an error if authentication is unsuccessful.

Definition

The login function accepts three parameters and returns a callback function:
This is a pseudo-JavaScript example of how you could implement the login function:

Encryption

Encrypt the password value using a cryptographic hash encryption library such as bcrypt to prevent any potential data leak. Avoid logging, storing, or transporting the password credential anywhere in its unencrypted form. For example:

Callback function

The callback function is used to pass user profile data or error data through the pipeline.

Definition

The callback function accepts up to two parameters and returns a function:

Return the user profile

The profile data returned by the Login script for a user must be consistent with the profile data returned by the Get User script.
If the user authenticates successfully, their profile data must be returned in the profile object in normalized form. For example:

WrongUsernameOrPasswordError type object

The WrongUsernameOrPasswordError custom error type object allows you to pass data that will be displayed in your Tenant Logs. The WrongUsernameOrPasswordError constructor accepts up to two parameters:
In addition to the standard fields, you can include the user_metadata, app_metadata, and mfa_factors fields.

Return an error

If an error occurs, the error parameter should contain relevant information about what went wrong. For example, you can return an error with a value for the userNameOrEmail field
In this case, Auth0 records an fp tenant log event. You can also return an error with a null value for the userNameOrEmail field:
In this case, Auth0 records an fu tenant log event.

Sync user profile attributes at each login

Enable the Sync user profile attributes at each login setting if you want Auth0 to update the name, nickname, given_name, family_name, and/or picture fields with the values returned from the external database on each login. If you do not enable this setting, the values returned from the external database the first time the user logs in will persist on subsequent logins, even if they’ve changed in the external database.

Language-specific script examples

Auth0 provides sample scripts for use with the following languages/technologies: