Actions Triggers: pre-user-registration

The pre-user-registration triggers runs when a user attempts to register through a Database or Passwordless connection. This trigger can be used to add metadata to the user profile before it is created or to deny a registration with custom logic.

Reference

  • Event object: Provides contextual information about the request to register a new user.

  • API object: Provides methods for changing the behavior of the flow

Common use cases

Deny registration by location

A pre-user-registration Action can be used to prevent a user from signing up.

/**
 * @param {Event} event - Details about registration event.
 * @param {PreUserRegistrationAPI} api
 */
exports.onExecutePreUserRegistration = async (event, api) => {
  if (event.request.geoip.continentCode === "NA") {

    // localize the error message 
    const LOCALIZED_MESSAGES = {
      en: 'You are not allowed to register.',
      es: 'No tienes permitido registrarte.'
    };

    const userMessage = LOCALIZED_MESSAGES[event.request.language] || LOCALIZED_MESSAGES['en'];
    api.access.deny('no_signups_from_north_america', userMessage);
  }
};

Set metadata in the user profile

A pre-user-registration Action can be used to add metadata to the user profile before it is created.

/**
 * @param {Event} event - Details about registration event.
 * @param {PreUserRegistrationAPI} api
 */
exports.onExecutePreUserRegistration = async (event, api) => {
  api.user.setUserMetadata("favorite_color", "purple");  
};

Store a user ID from another system in the user profile

A pre-user-registration Action can be used to store a user ID from another system in the user profile.

const axios = require("axios");

/**
 * @param {Event} event - Details about registration event.
 * @param {PreUserRegistrationAPI} api
 */
exports.onExecutePreUserRegistration = async (event, api) => {
  const remoteUser = await axios.get("https://my-api.exampleco.com/users", { params: { email: event.user.email }});

  if (remoteUser) {
    api.user.setAppMetadata("my-api-user-id", remoteUser.id); 
  }
};

To use an NPM library like axios, you must add the library as a dependency to the Action.