kevin strong-holte

All content in this article is for demonstrative purposes only. Any resemblance to existing companies, products, or features is coincidental and does not imply ownership or association.

Extensibility Feature SDK / Methods / Get User

The Get User method implements the function executed to determine the current state of existence of a user. We recommend naming this function getUser.

This script is required for automatic migration, and conditionally required for legacy authentication depending on the operations configured for the connection. To avoid creating duplicate users, set a consistent, unchanging user_id on the returned user profile for the same user.

If automatic migration is configured for the connection and the user profile has not yet been created, the script is executed whenever any of the following operations occur:

If legacy authentication is configured for the connection, the script is executed whenever any of the following operations occur:

Get User function

The getUser function should:

Definition

The getUser function accepts two parameters and returns a callback function:

getUser(email, callback): function

Parameters

email string required

The user's email address.

callback function required

Used to pass error or profile data through the pipeline.

Example

This is a pseudo-Javascript example of how you could implement the getUser function.

1function getUser(email, callback) {
2 // Send user identifier to external database API
3 let options = {
4 url: "https://example.com/api/search-users",
5 body: {
6 email: email
7 }
8 };
9
10 send(options, (err, profileData) => {
11 // Return error in callback if there was an issue finding the user
12 if (err) {
13 return callback(new Error("Could not determine if user exists or not."));
14 } else {
15 // Return null in callback if user was not found, return profile data in callback if user was found
16 if (!profileData) {
17 return callback(null);
18 } else {
19 let profile = {
20 email: profileData.email,
21 user_id: profileData.userId
22 };
23
24 return callback(null, profile);
25 }
26 }
27 });
28}

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:

callback(error[,profile]): function

Parameters

error object required

Contains error data.

profile object optional

Contains the user's profile data.

Return the user profile (user found)

The profile data returned by the Get User script for a user must be consistent with the profile data returned by the Login script.

If the user is found, pass a null value for the error parameter, and pass the user's profile data for the profile parameter in normalized form. In addition to the standard fields, you can include the user_metadata, app_metadata, and mfa_factors fields.

Example

1return callback(null, {
2 username: "username",
3 user_id: "my-custom-db|username@domain.com",
4 email: "username@domain.com",
5 email_verified: false,
6 user_metadata: {
7 language: "en"
8 },
9 app_metadata: {
10 plan: "full"
11 },
12 mfa_factors: [
13 {
14 phone: {
15 value: "+15551234567"
16 }
17 },
18 ]
19});

Return no user profile (user not found)

If the user is not found, pass a null value for the error parameter, and omit the profile parameter.

Example

return callback(null);

Return an error

If an error occurs, pass an Error object instance for the error parameter with relevant information about what went wrong. For more information, read Troubleshoot Custom Databases.

Example

return callback(new Error("My custom error message."));