Automatically resend verification email upon login when link expires

Problem Statement

If the email address verification link that was sent in the verification email has expired, I want to automatically resend an email to a user on their next login.

Solution

There is no out-of-the-box way to do that. However, you could achieve it with a Rule or an Action.

You will need to take into account the link expiration you have set in the email template, and in the Rule/Action you would probably want to check for the user’s last login event and see if the email_verified attribute is set to true or false.

For example, in a post-login Action you would do something like this:

exports.onExecutePostLogin = async (event, api) => {

  var ManagementClient = require('auth0').ManagementClient;
  var management = new ManagementClient({
    domain: event.secrets.domain,
    clientId: event.secrets.clientId,
    clientSecret: event.secrets.clientSecret
  });

   function daydiff(first, second) {
    return (second - first) / (1000 * 60 * 60 * 24);
  }

  // Changes to last_login are considered updates, so most of the time, updated_at will match last_login.
  const last_login = event.user.updated_at;
  const diff = daydiff(new Date(last_login), new Date())
  const verificationDaysLimit = 0.0; // <- SET HERE THE TIME FOR THE VERIFICATION LINK TO EXPIRE

  const verified = event.user.email_verified;

  if (diff > verificationDaysLimit && !verified) {

    var params = {
      client_id: event.client.client_id,
      user_id: event.user.user_id
    };

    management.jobs.verifyEmail(params, function (err) {
      if (err) {
      // Handle error.
      }
    });
  }

};

Remember that you need to add the secrets (environment variables) like this: Write Your First Action
And also the dependencies (in this case, the ‘auth0’ library):