Send Change Password Email from Post-User Registration Action

Problem statement

Are there any Action samples that show how to send a change password email after a user signs up?

Solution

The following Post-User-Registration Action code will send a change password email once a user signs up:

exports.onExecutePostUserRegistration = async (event, api) => {
  
const axios = require('axios');

  const domain = '{YOUR_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' 
      }, {
        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);
  }
}