Send a Verification Email from a Post-Login Action

Overview

This article provides steps for configuring a Post-Login Action to automatically send a verification email to users who have not yet verified their email address.

Applies To
  • Verification Email
  • Post-Login Action
Solution

NOTE: This solution is a potential approach and may require modification for specific use cases. Always test new Actions or integrations in a non-production environment before deploying to production.

  1. Ensure that the clientId and clientSecret variables in the Action are configured as secrets. These values must come from a Machine-to-Machine (M2M) application, such as the API Explorer Application, which has the necessary permissions to use the Management API. A token from this application is required for the email verification job to function within the Action. For more details, see Add Action Secrets.

  2. Add the following code to the onExecutePostLogin trigger in the Action: 

exports.onExecutePostLogin = async (event, api) => {
  const ManagementClient = require('auth0').ManagementClient;
  const management = new ManagementClient({
    domain: event.secrets.domain,
    clientId: event.secrets.clientId,
    clientSecret: event.secrets.clientSecret
  });

  const verified = event.user.email_verified;

  // I am slicing off the 'auth0|' prefix of the user_id here
  const userIdWithoutAuth0 = event.user.user_id.slice(6);

  const params = {
    client_id: event.client.client_id,
    user_id: event.user.user_id,
    identity: {
      // Passing the corrected user_id here
      user_id: userIdWithoutAuth0,
      provider: 'auth0'
    }
  };

  if (!verified) {
    management.jobs.verifyEmail(params, function (err) {
      if (err) {
        console.log(err)
      }
    });
  }
};


Further reference to the available methods for the ManagementClient can be found here.

Recommended content

No recommended content found...