Sending Password Change Email for New Users

Overview

In Auth0, when a new user is manually added, a verification email is typically sent to confirm the user’s email address. However, in certain scenarios, it may be preferred to send a password change email instead, allowing the user to set their password upon registration.

Applies To

  • Auth0 Tenant Administrators
  • Welcome Email

Solution

To send a password change email upon user registration, follow these steps:

  • Go to the Actions section in the Auth0 Dashboard
  • Select Triggers and then Post-User Registration
  • Create a new Action
  • In the action editor, add the following code:
exports.onExecutePostUserRegistration = async (event, api) => {
  const axios = require("axios");
  const domain = "{your_domain}"; // Replace with your Auth0 domain

  // Function to send password change email
  const sendPasswordChangeEmail = async (email) => {
    try {
      await axios.post(
        `https://${domain}/dbconnections/change_password`,
        {
          email: email,
          connection: "Username-Password-Authentication", // Replace with your connection name
        },
        {
          headers: {
            "Content-Type": "application/json",
          },
        }
      );
      console.log("Password change email sent successfully.");
    } catch (error) {
      console.error("Error sending password change email:", error);
      throw error;
    }
  }; 

  // Execute the function
  try {
    await sendPasswordChangeEmail(event.user.email);
  } catch (error) {
    console.error("Error in post user registration action:", error);
  }
};
  • Replace {your_domain} with the Auth0 domain and ensure the connection name matches your setup.
  • In the action editor, click on the Dependencies tab.
  • Click Add Dependency and search for Axios.
  • Select the latest version and click Add.
  • Save and deploy the action.