See the roles of users in the accestoken

How can I see the roles in my user token when I log into my react application with auth0? Now I have the ESSENTIALS plan.

Hi @auth0-ikearg,

Welcome to the Auth0 Community!

If you require getting the user’s role in your access token, you need to use the Management API in a Post-Login Actions to append the user’s role to the Access token.

I recommend checking out our How can I use the Management API in Actions? documentation as a starting point.

Please let me know if you have any questions or need more help with the implementation.

Thanks,
Rueben

1 Like

Thanks for the help!
In my case i created this rule to add the user role in my token:

function (user, context, callback) {
  const namespace = 'http://localhost:5173';
  const assignedRoles = (context.authorization || {}).roles;

  let idTokenClaims = context.idToken || {};
  let accessTokenClaims = context.accessToken || {};

  idTokenClaims[`${namespace}/roles`] = assignedRoles;
  accessTokenClaims[`${namespace}/roles`] = assignedRoles;

  context.idToken = idTokenClaims;
  context.accessToken = accessTokenClaims;

  callback(null, user, context);
}
1 Like

Hi @auth0-ikearg,

Thanks for the response.

I’m glad you got it working by using an Auth0 Rule!

However, I will draw your attention to the fact that Auth0 Rules is reaching its end of life in the near future, on November 18, 2024. Because of this, we strongly recommend using Auth0 Actions to avoid any disruptions. You should also see a warning banner on the Auth0 Dashboard > Auth0 Pipeline > Rules page mentioning this:

Thanks,
Rueben

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.