I have set a custom Action which appends Roles to ID Token as a Custom Claim:
exports.onExecutePostLogin = async (event, api) => {
if (event.authorization) {
api.idToken.setCustomClaim('rolesArray', event.authorization.roles);
}
}
This action runs after a custom action which assigns a default role as seen in a video given by Auth0
exports.onExecutePostLogin = async (event, api) => {
if(event.authorization &&
event.authorization.roles &&
event.authorization.roles.length===0){
const ManagementClient = require('auth0').ManagementClient;
const management = new ManagementClient({
domain: event.secrets.domain,
clientId: event.secrets.clientID,
clientSecret: event.secrets.clientSecret,
});
const params = {id:event.user.user_id};
const data = {"roles":[<id of default role>]};
try{
await management.users.assignRoles(params,data);
}catch(e){
console.log(e)
}
}
};
Both of these actions are in the Login Flow in the following order:
DefaultRoleAssign → AppendRoleToIDToken
The problem is that even though the default role is assigned(I can see it in user management of Auth0 Dashboard) whenever a new user signs in with Google, the roles are not fetched to the application and the customClaim is an empty array. The roles are available in subsequent logins. How can I also ensure that the roles are fetched in the first login?
Thank you!