Read from roles useAuth0 SDK hook

  • SDK: auth0-react
  • SDK Version: 1.8.0
  • Platform Version: Node 16.13.1

My very simple goal is to push role information onto a user, then use that role information in my app. I was able to accomplish the first step. I now see assigned roles when I log the user object.

image

How do I now check the user roles? The array containing the roles seems to have an inaccessible namespace. What do I need to do programmatically determine, “Is the user in X role?” What roles is Y user assigned?

Hi @charlie,

Thanks for reaching out to the Auth0 Community!

First, could you please clarify how you assigned the Roles to your users? Did you use the Management API?

And could you clarify if you saved the Roles using RBAC or just directly to the user’s profile?

Thank you.

Happy to clarify. Roles are assigned to a user’s profile in the Dashboard. Then, I am adding user roles to a token in a post-login trigger.

Is this the best approach?

Hi @charlie,

Thank you for your response.

First, to check the user’s Roles you can use the Management API v2 Get user role endpoint to see the list of Roles assigned to them. This is one approach for getting the user’s roles.

You could also use it in conjunction with the Get a user’s permissions endpoint to determine the authorization granted to the user.

Hoped this helps!

Please let me know how this works for you.

Thank you.

Thanks Rueben. I’m already connected with the auth0-react SDK. Is there nothing in the SDK to verify role assignment? I’m looping over the array containing the roles, but it feels like a hack.

Hi @charlie,

Thank you for your response and clarification.

After checking the Auth0 React SDK’s useAuth0 hook, you could use the getIdTokenClaims method to get the user’s roles after appending them as a custom claim using the Post-Login Action as you mentioned earlier.

This way, you could programmatically check the list of roles assigned to the user. Doing so is the preferred approach to check for the user’s roles.

Does that help?

Thanks.

Thanks for hanging in there with me, @rueben.tiow. I can’t seem to locate that method in the useAuth0 hook docs. Trying this throws an error:

const claims = useAuth0.getIdTokenClaims()
console.log(claims)

Are we sure this method is available in the Auth0 React SDK’s useAuth0 hook?

_auth0_auth0_react__WEBPACK_IMPORTED_MODULE_1__.useAuth0.getIdTokenClaims is not a function

Hi @charlie,

Thank you for the response.

Please take a look at https://auth0.github.io/auth0-react/modules/use_auth0.html for the reference on getIdTokenClaims.

Let me know how this works for you.

Thanks.

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