getUser.
This script is required for automatic migration to Auth0, and conditionally required without migration enabled depending on the operations configured for the connection. Auth0 strongly recommends that the user_id you return on the user profile be permanent and consistent: for a given user, the Get User script should return the same user_id on every execution. See Return a consistent user_id for details.
When Import Users to Auth0 is enabled, the Get User script runs when a user attempts to sign up to check if the user already exists in the external user store.
The Get User script also runs when a user attempts to:
- Change their email address (Change Email script)
- Log in (Login script)
- Change their password (Change Password script)
- Sign up (Create script)
- Change their email address (Change Email script)
- Change or reset their password (Change Password script)
Get User function
ThegetUser function should:
- Send the user’s identifier to the external database’s API.
- Return the profile data of the user if the user was found.
- Return an error if there was an issue determining whether the user exists or not.
Definition
ThegetUser function accepts two parameters and returns a callback function:
This is a pseudo-JavaScript example of how you could implement the
getUser function:
Callback function
Thecallback function is used to pass user profile data or error data through the pipeline.
Definition
Thecallback function accepts up to two parameters and returns a function:
Return the user profile (user found)
If the user is found, pass anull value to the error parameter, and pass the user’s profile data to the profile parameter in normalized form:
user_metadata, app_metadata, and mfa_factors fields.
Return a consistent user_id
Auth0 uses theuser_id returned by the Get User script to identify the user across your tenant. For a given user, this value should be consistent: the Get User script should return the same user_id every time it runs for that user. Derive the user_id from a stable, permanent property of your source record (such as the database primary key), not from a value that changes between executions.
Returning an inconsistent
user_id can create duplicate or orphaned users, and can break flows that rely on the identifier remaining stable.user_id from a stable property of the source record, so the same user always maps to the same value.
user_id on each call. For example, returning a freshly generated UUID. This produces a different identifier every time the script runs for the same user.
Return no user profile (user not found)
If the user is not found, pass anull value to the error parameter, and omit the profile parameter:
Return an error
If an error occurs, pass error data to theerror parameter with relevant information about what went wrong: