My action code seems not to be executed

I want to re-send the verify email to users who login, and have not yet verified their email.

I’ve configured a new Machine to Machine application, accordingly to the steps illustrated in this post, and given the new application access to update:users scope.

I deployed a new action with this code:

const { ManagementClient } = require('auth0');

/**
 * Handler that will be called during the execution of a PostLogin flow.
 *
 * @param {Event} event - Details about the user and the context in which they are logging in.
 * @param {PostLoginAPI} api - Interface whose methods can be used to change the behavior of the login.
 */
exports.onExecutePostLogin = async (event, api) => {
  const { user } = event;
  if (!user.email_verified) {
    const client = new ManagementClient({
      domain: event.secrets.domain,
      clientId: event.secrets.clientId,
      clientSecret: event.secrets.clientSecret,
    });

    try {
      await client.sendEmailVerification({ user_id: user.user_id });
      console.log('Verification email resent for user:', user.email);
    } catch (error) {
      console.error('Failed to resend verification email:', error);
    }
  }
};

When I test it, I don’t receive any email (checked Spam folder as well).

Here, I read that I should be able to see a new section, called “Action details” in the Monitoring / Logs page for successfull logins; but this is not the case for me. This didn’t change also after I deployed a simplified version of the action:

exports.onExecutePostLogin = async (event, api) => {
  console.log("My custom action is running");
};

I also tested the core functionality of the action locally, with the function you can see below:

const { ManagementClient } = require("auth0");

async function test () {
  const client = new ManagementClient({
    domain: event.secrets.domain,
    clientId: event.secrets.clientId,
    clientSecret: event.secrets.clientSecret,
  });

  try {
    await client.sendEmailVerification({ user_id: "********" });
    console.log("Success!");
  } catch (error) {
    console.error("Error:", error);
  }
};

test();

… and in this case I was able to receive the email; so it really seems like the action code is not running; any idea why this could be the case? Or how could investigate this further?

Any help is appreciated - thank you.

Hey @brunoscopelliti welcome to the community, and thanks for the detailed write up :slight_smile:

Good to know the code is working fine locally, that’s odd it’s seemingly not executing in the context of the Action. I just glanced at your tenant and it looks like the Action isn’t bound to the login flow, I’m not sure if this is on purpose or not - To bind it to the flow you need to go to your dashboard Actions → Flows → Login and add it there:

The real time webtask logs extension may be useful in troubleshooting as well - I just tested on my end with a simple console.log() in an Action and could see it successfully execute/print out there.

Keep us posted!

Hey @tyf

Thank you for the quick answer.
It was exactly that, I must have missed that part from the docs, and the “Deploy” button in the Action editor page made me think that my changes were live, when this wasn’t the case.

Bests,

1 Like

Agreed, that wording isn’t the best - Anyways glad it’s working for you now and thanks for following up with the community!

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