I am new to Auth0 and I have created a new action in the library with the following code. But when I try to test it it’s giving me errors. Can someone please help me, if I’m doing something wrong?
Action Type: Post User Registration
Runtime: Node 22
/**
* Handler that will be called during the execution of a PostUserRegistration flow.
*
* @param {Event} event - Details about the context and user that has registered.
* @param {PostUserRegistrationAPI} api - Methods and utilities to help change the behavior after a signup.
*/
exports.onExecutePostUserRegistration = async (event, api) => {
const axios = require('axios');
const tokenResponse = await axios.post(`https://${event.secrets.AUTH0_DOMAIN}/oauth/token`, {
client_id: event.secrets.MGMT_CLIENT_ID,
client_secret: event.secrets.MGMT_CLIENT_SECRET,
audience: `https://${event.secrets.AUTH0_DOMAIN}/api/v2/`,
grant_type: 'client_credentials',
}
);
const token = tokenResponse.data.access_token;
await axios.post(
`https://${event.secrets.AUTH0_DOMAIN}/api/v2/users/${event.user.user_id}/roles`,
{ roles: [event.secrets.DEFAULT_ROLE_ID] },
{
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
'Accept-Encoding': 'gzip, deflate'
}
}
);
};