Post login trigger not working as expected

I am migrating data from keycloak to auth0, As per auth0 email format is not allowed in username field. Due to this i have imported username as login_identifier in app_metadata
Refer to this below

[
    {
        "user_id" : "79b72656-7a50-4214-985e-fa537efe85e0",
        "given_name": "test",
        "family_name": "user",
        "email": "test1@gmail.com",
        "email_verified": false,
        "custom_password_hash": {
            "algorithm": "pbkdf2",
            "hash": {
                "value": "$pbkdf2-sha256$i=27500,l=64$5TXiLHDApo9xDKVk1Qsarw$D4VVb7Uo0QIo/4vulomwT43Sn99hMf3d5LaB8Ufi3BXPkZrWF7rcAdN+u3miltoYvkJIx2//roiDpvkdI+fU3A"
            }
         },
         "app_metadata": {
            "login_identifier": "test123@gmail.com"
          }
    }
]

I am trying to ensure that this login_identifier is used for login instead of email.

I have written a custom action : VALIDATE_USERNAME

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

// Extract the entered username (email-like) from the request
  const enteredUsername = event.request.body.email;

  // Extract the stored username from app_metadata
  const storedUsername = event.user.app_metadata?.login_identifier;

  // Check if entered username matches stored username
  if (storedUsername != null && enteredUsername === storedUsername) {
    // Allow login to proceed
    return;
  }
  // Deny login if username doesn't match
  throw new Error('Invalid username or password here in customs.');

};

After creating this action, i have added it to trigger : post-login

But this is not working as expected, Why is this post login trigger not working ?

Hi @purva.jantikar,

I have just tested your post-login action script and got it to run. Some things you could check is in your Auth0 Logs to verify that the Action details in your login event executed.

Additionally, I recommend using the built-in actions debugger to test your action scripts before deployment. For more information, see our Test Actions documentation.

If you prefer to test in a legitimate login flow, you can use the Real-time Webtask Logs Extension.

Thanks,
Rueben

1 Like