I would like to create a post user registration action that sends a password reset email only if the user is created from the Auth0 management API, not the universal login. Is there a way to determine the source of the action call?
Hey there @Sbm welcome to the community!
The easiest way to go about this might be to just add a flag in user metadata when creating users via the API - This way you can check for the flag in the post registration action and implement any necessary logic:
Create User with "api": true
flag in user_metadata
curl -L 'https://{}your_domain}.us.auth0.com/api/v2/users' \
-H 'Content-Type: application/json' \
-H 'Accept: application/json' \
-H 'Authorization: Bearer {management_api_access_token}' \
--data-raw '{"email":"test@example.com","user_metadata":{"api":true},"blocked":false,"email_verified":false,"app_metadata":{},"given_name":"foo","family_name":"bar","name":"foo","nickname":"foobar","connection":"Username-Password-Authentication","password":"xxxx","verify_email":false,"username":"foobar1"}'
Example post registration action:
exports.onExecutePostUserRegistration = async (event, api) => {
const ManagementClient = require('auth0').ManagementClient;
// Initialize management client
const management = new ManagementClient({
domain: event.secrets.domain,
clientId: event.secrets.clientID,
clientSecret: event.secrets.clientSecret,
});
// Define the user's ID
const userId = event.user.user_id;
if (event.user.user_metadata.api === true) {
// Send a password reset email
management.requestChangePasswordEmail({ user_id: userId }, (err) => {
if (err) {
console.error('Error sending password reset email:', err);
} else {
console.log('Password reset email sent successfully.');
}
});
}
}
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.