Send a Verification Email from a Post-Login Action
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.
- Verification Email
- Post-Login Action
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.
-
Ensure that the
clientIdandclientSecretvariables 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. -
Add the following code to the
onExecutePostLogintrigger 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.