Disable Email Verification In Pre-User-Registration Trigger

I’m trying to prevent the user email verification for users in a pre-registration trigger. The idea is that I want to pass a flag to the trigger in the meta data that would allow us to automatically verify the users email and NOT send the verification email.

Here’s what I want to do in psuedo code:

if ( event.user.user_metadata?.SkipEmailVerification == true )
{
event.user.verify_email = false;
event.user.email_verfied = true;
}

I’m unable to find a way to modify these properties in my trigger.

Thanks

You are trying to update user fields before user is even registered using pre-user-registration action. I am not sure, if this is even possible. You can likely use post-registration-action and update user call to set this field to verified.

// Use a post-registration trigger instead
// This is often more permissive with user modifications
exports.postRegistrationHandler = async (event, context) => {
  if (event.user.user_metadata?.skipEmailVerification) {
    // Use the link above to build your auth0 update API call.
    await auth.updateUser(event.user.id, {
      emailVerified: true
    });
  }
};

Thanks Suman,

It makes sense what you are saying about the prereg trigger. I’ll try what you’ve suggested in my exports.onExecutePostUserRegistration trigger.

I’m having an issue with the update user method. Here’s my code

    const ManagementClient = auth0Sdk.ManagementClient;
       
    const management = new ManagementClient({
      domain: event.secrets.DOMAIN,
      clientId: event.secrets.CLIENT_ID,
      clientSecret: event.secrets.CLIENT_SECRET,
      audience: 'https://' + event.secrets.DOMAIN + '/api/v2/',
      tokenProvider: {
        enableCache: true,
        cacheTTLInSeconds: 10
      }
    });

    const params =  { id: event.user.user_id};

    const data = {
      emailVerified: true 
    }
 
 
    try {
        const res = await management.updateUser(params, data) 
      } 
      catch (e) {
        console.log(" ERROR: " + e)
 
      }
 

But I’m getting the following error: ERROR: TypeError: management.updateUser is not a function.

Any ideas on what wrong here, would be appreciated.

Hi @jibrazil,

Unfortunately, as @sumansaurav mentioned, you won’t be able to disable the email verification using a pre-user registration Action. Additionally, the post-user registration Action will not work here either since it happens after the user is created.

Note that after a user is created, it will automatically send the welcome email and verification email (if enabled). Therefore, the only way to ensure that the verification email is not sent is to create the users manually using the Management API’s create a user endpoint and passing in the verify_email=false body parameter.

Thanks,
Rueben

2 Likes