Send email from Actions

Hi @jeanbaptiste.marie,

Welcome to the Auth0 Community!

I understand that you are trying to send a password reset email to a user whenever they log in and have not changed their password for a given time.

For this, I recommend using a Post-Login Action with the Authentication API, specifically calling the POST /dbconnections/change_password endpoint.

I have written a code snippet below to make an Axios request to the Change Password endpoint. From here, you will need to write the code to compare the user’s last password change to a given time, and if it satisfies, you can call the sendPasswordResetEmail() function.

exports.onExecutePostLogin = async (event, api) => {
  var axios = require("axios").default;

  const sendPasswordResetEmail = () => {
    var options = {
      method: 'POST',
      url: 'https://YOUR_DOMAIN}/dbconnections/change_password',
      headers: {'content-type': 'application/json'},
      data: {
        client_id: 'event.client.client_id',
        email: 'event.user.email',
        connection: 'event.connection.name'
        
      }
    };
    try{
    axios.request(options).then(function (response) {
      console.log("Response: ",response.data);
    })
    }catch(error){
      console.error(error)
    }
  }
  /*
  Check if the user has not changed their password for a given time.
  If this is true, call the sendPasswordResetEmail() function.
 */
}

Please let me know how this goes for you.

Thanks,
Rueben