How to resend verification email on failed login

I’m trying to run the following action in my login flow:

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

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


    try{
       management.jobs.verifyEmail(params, function (err) {
      if (err) {
        console.error('Error sending verification email:', err);
      } else {
        console.log('Verification email sent successfully');
      }
    });
    }catch(e){
      console.error("Error:: ",e)
    }
  
    api.access.deny("Please verify your email before logging in.");
  }
}

I have included the auth0 dependency in the flow and all the necessary secrets. However, I get the following error in the web task logs:

Error sending verification email: {
  init: {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: {
      client_id: 'XXX',
      user_id: 'XXXXX'
    },
    agent: undefined
  },
  context: {
    path: '/jobs/verification-email',
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: {
      client_id: 'XXXXXX',
      user_id: 'XXXXXX'
    }
  }
}
9:56:39 AM:
 finished webtask request
9:56:39 AM:
 {
  "code": 500,
  "error": "Script generated an unhandled asynchronous exception.",
  "details": "FetchError: The request failed and the interceptors did not return an alternative response",
  "name": "FetchError",
  "message": "The request failed and the interceptors did not return an alternative response",
  "stack": "FetchError: The request failed and the interceptors did not return an alternative response\n    at BaseAPI.fetch (/data/layers/layers-C3xn/C3xnOYIlqF189uh2kQDl_3D_mHnh1WGE-qV7kWUz3VY/node_modules/auth0/dist/cjs/lib/runtime.js:84:27)\n    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)\n    at async OAuth.request (/data/layers/layers-C3xn/C3xnOYIlqF189uh2kQDl_3D_mHnh1WGE-qV7kWUz3VY/node_modules/auth0/dist/cjs/lib/runtime.js:115:26)\n    at async grant (/data/layers/layers-C3xn/C3xnOYIlqF189uh2kQDl_3D_mHnh1WGE-qV7kWUz3VY/node_modules/auth0/dist/cjs/auth/base-auth-api.js:84:22)\n    at async TokenProvider.getAccessToken (/data/layers/layers-C3xn/C3xnOYIlqF189uh2kQDl_3D_mHnh1WGE-qV7kWUz3VY/node_modules/auth0/dist/cjs/management/token-provider.js:20:85)\n    at async TokenProviderMiddleware.pre (/data/layers/layers-C3xn/C3xnOYIlqF189uh2kQDl_3D_mHnh1WGE-qV7kWUz3VY/node_modules/auth0/dist/cjs/management/token-provider-middleware.js:26:23)\n    at async BaseAPI.fetch (/data/layers/layers-C3xn/C3xnOYIlqF189uh2kQDl_3D_mHnh1WGE-qV7kWUz3VY/node_modules/auth0/dist/cjs/lib/runtime.js:52:26)\n    at async JobsManager.request (/data/layers/layers-C3xn/C3xnOYIlqF189uh2kQDl_3D_mHnh1WGE-qV7kWUz3VY/node_modules/auth0/dist/cjs/lib/runtime.js:115:26)\n    at async JobsManager.verifyEmail (/data/layers/layers-C3xn/C3xnOYIlqF189uh2kQDl_3D_mHnh1WGE-qV7kWUz3VY/node_modules/auth0/dist/cjs/management/__generated/managers/jobs-manager.js:121:26)"
}

This tells me there was an error but that’s it. The documentation does not provide any insight to this issue.

When I try to do this flow in my node server API it works fine and its practically the same code.

Hi @smd,

From my initial investigation, your Action script looks good. I do not see an issue that could be contributing to the error you saw.

I have also checked your tenant logs and see that the user is being denied access accordingly.
Can you confirm if you are getting the verification email?

And, have you tried using the Actions built-in debugger to test out your script to see if you have any issues?

(Reference: Test Actions)

Thanks,
Rueben

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.