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:
ParameterTypeDescription
userNameOrEmailStringThe user’s username or email.
passwordStringThe user’s password in plain text.
callbackFunctionUsed to pass error or profile data through the pipeline.

Example

This is a pseudo-JavaScript example of how you could implement the login function. For language-specific examples, read Language-specific script examples.

Encryption

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

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:
ParameterTypeRequiredDescription
errorObjectRequiredContains error data.
profileObjectOptionalContains the user’s profile data.

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. In addition to the standard fields, you can include the user_metadata, app_metadata, and mfa_factors fields.

Example

Return an error

If an error occurs, the error parameter should contain relevant information about what went wrong.

WrongUsernameOrPasswordError type object

The WrongUsernameOrPasswordError custom error type object allows you to pass data that will be displayed in your Tenant Logs.
Constructor
The WrongUsernameOrPasswordError constructor accepts up to two parameters:
ParameterTypeRequiredDescription
userNameOrEmailStringRequiredContains the user’s username or email, or a null value.
messageStringOptionalContains information about the error.

Return error with username or email

If you return an error with a value for the userNameOrEmail field, Auth0 will record an fp tenant log event.
Example
Tenant Log Event FieldValue
Codefp
EventFailed Login (Incorrect Password)
DescriptionMy custom error message

Return error without username or email

If you return an error with a null value for the userNameOrEmail field, Auth0 will record an fu tenant log event.
Example
Tenant Log Event FieldValue
Codefu
EventFailed Login (Invalid Email/Username)
DescriptionMy custom error message

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:

JavaScript

ASP.NET Membership Provider (MVC3 - Universal Providers)

ASP.NET Membership Provider (MVC4 - Simple Membership)

MongoDB

MySQL

PostgreSQL

SQL Server

Windows Azure SQL Database

Axios

Stormpath

Learn more