# How to Trigger Password Reset From Post User Login Action

**URL:** <https://community.auth0.com/t/how-to-trigger-password-reset-from-post-user-login-action/127976>\
**Category:** Get Help\
**Tags:** users, management-api\
**Created:** [February 21, 2024, 4:30pm UTC](https://community.auth0.com/t/how-to-trigger-password-reset-from-post-user-login-action/127976 "2024-02-21T16:30:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![rot](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rot/32/70866_2.png) [@rot](https://community.auth0.com/u/rot)\
**Post date:** [February 21, 2024, 4:30pm UTC](https://community.auth0.com/t/how-to-trigger-password-reset-from-post-user-login-action/127976/1 "2024-02-21T16:30:21Z")

</div>

Hi,

Is this possible? I’m checking for the value of an app attribute in the profile, and if true I want to trigger the Password Rest email to them.

Thanks

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [February 21, 2024, 4:55pm UTC](https://community.auth0.com/t/how-to-trigger-password-reset-from-post-user-login-action/127976/3 "2024-02-21T16:55:18Z")

</div>

Hey @rot welcome to the community!

> [@rot](#):
>
> I’m checking for the value of an app attribute in the profile, and if true I want to trigger the Password Rest email to them.

This should definitely be possible - In the following example I’m using the [node management client](https://github.com/auth0/node-auth0) to check for a specific flag in a user’s [app\_metadata](https://auth0.com/docs/manage-users/user-accounts/metadata) and triggering a password reset email accordingly:

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

exports.onExecutePostLogin = async (event, api) => {
  // Initialize the Management Client
  const management = new ManagementClient({
    token: event.secrets.MANAGEMENT_API_TOKEN,
    domain: event.secrets.DOMAIN
  });

  // Check the app_metadata directly
  if (event.user.app_metadata && event.user.app_metadata.triggerPasswordReset) {
    // Trigger password reset email
    await management.sendPasswordChangeEmail({ user_id: event.user.user_id });
  }
};

```

---

<div class="post-metadata">

**Author:** ![rot](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rot/32/70866_2.png) [@rot](https://community.auth0.com/u/rot)\
**Post date:** [February 22, 2024, 5:23pm UTC](https://community.auth0.com/t/how-to-trigger-password-reset-from-post-user-login-action/127976/4 "2024-02-22T17:23:20Z")

</div>

Thanks, so it doesn’t seem to be triggering any email. It is falling into the if statement correctly though. No errors when testing it either 😕

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [February 22, 2024, 10:41pm UTC](https://community.auth0.com/t/how-to-trigger-password-reset-from-post-user-login-action/127976/6 "2024-02-22T22:41:08Z")

</div>

Thanks for the heads up! My apologies, it looks like that example may have worked in the past but the way in which you trigger a password reset email has changed 😕 The following should be accurate and up to date:

```auto
const AuthenticationClient = require('auth0').AuthenticationClient;

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

    var auth0 = new AuthenticationClient({
        domain: event.secrets.AUTH0_DOMAIN,
        clientId: event.secrets.CLIENT_ID,
        clientSecret: event.secrets.CLIENT_SECRET
      });

    const data = {
        email: event.user.email, // The user's email address
        connection: 'Username-Password-Authentication', // The name of your database connection in Auth0 (only works for database connections)
      };
      
    auth0.database.changePassword(data)
        .then(response => {
          console.log('Password change email sent:', response);
        })
        .catch(error => {
          console.error('Error sending password change email:', error);
        });
}

```

---

<div class="post-metadata">

**Author:** ![rot](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rot/32/70866_2.png) [@rot](https://community.auth0.com/u/rot)\
**Post date:** [February 23, 2024, 3:47pm UTC](https://community.auth0.com/t/how-to-trigger-password-reset-from-post-user-login-action/127976/7 "2024-02-23T15:47:05Z")

</div>

Ok so… Yes the code works thank you.

The issue I have now is when it runs, what I actually wanted to happened was for this to run when a user tries to login and fails (because they are newly migrated and therefore do not have a password set).

However it seems the Login action only runs Post-Login. I wonder if my use case is possible or not…? Where it would run Post-Login-Fail…

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [February 23, 2024, 11:49pm UTC](https://community.auth0.com/t/how-to-trigger-password-reset-from-post-user-login-action/127976/8 "2024-02-23T23:49:43Z")

</div>

Ah, gotcha! Thanks for clarifying - It sounds like the following is what you are looking for:

> [@Feature: pre-login Action (for email/password-based login attempts)](https://community.auth0.com/t/feature-pre-login-action-for-email-password-based-login-attempts/82725):
>
> Description: A pre-login Action for email/password-based login attempts would be very, very helpful for us for achieving user migration over to Auth0. We would like to be able to have an action executed before the password entered by users is verified. Use-case: Our use-case is user migration. In the pre-login Action, we would like to start a re-authentication process that ends with the user being able to set a password for their not yet activated account. We imagine the pre-login Action in our…

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [March 8, 2024, 11:50pm UTC](https://community.auth0.com/t/how-to-trigger-password-reset-from-post-user-login-action/127976/9 "2024-03-08T23:50:25Z")

</div>

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