I have free auth0 account and it’s great. I have it integrated with my react application and it’s working fine. I have 2 actions defined and here is what each of them does.
1st action that runs inside ‘Post-Login’ trigger: Assign ‘User’ Role to everyone that registers to my application via auth0.
the code for the action is below
exports.onExecutePostLogin = async (event, api) => {
if (event.stats.logins_count !== 1) {
return;
}
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" : [event.secrets.UserRoleID]};
try {
await management.users.assignRoles(params, data);
} catch (e) {
console.log(e);
}
};
The code works fine and every user that registers does indeed get assigned the ‘User’ role that i have created in auth0
2nd action, creates a role property and attaches it to the user token that auth0 issues. ( this runs directly after the 1st action above also in on post-login trigger )
Code for 2nd action below
exports.onExecutePostLogin = async (event, api) => {
const namespace = "https:/my-cool-name-space";
const assignedRoles = event.authorization?.roles || [];
api.idToken.setCustomClaim(`${namespace}/roles`, assignedRoles);
};
The issue that i’m seeing is that when a user register and in auth0 i can see them having the ‘user’ role, but in my react app when i am using auth0 Provider and extracting the user object from auth0 i am not seeing the roles in there on the VERY FIRST TIME THEY REGISTER AND AUTHENTICATE In my app.
If they logout, and logback in, i can see the roles object and i am able to extract it in my code and work with it…
I have tried the following
Making the action to work inside ‘Post-registeration’ trigger but that didn’t seem to work at all. i couldn’t even see the role being assigned in auth0.
I have tried adding both of the actions into 1 but that also didn’t send the roles property in the user object the first time.
Is there anything i’m missing ?
NOTE: I do see the role property but it’s empty. Which leads me to think there is a race conditon between my 1st action trigger and the 2nd one, where the user gets assigned the role but the token is already generated and doesn’t have the roles in it so therefore it defaults to the empty array i have defined.
I have tried refetching a token silently from my react client app but that didn’t also work.
The only way it works is if i log out and log back into my app…
Any guidance on this would be greatly apprecaited!