# Unable to Retrieve Custom "Roles" Claim from Access Token at /userinfo Endpoint

**URL:** <https://community.auth0.com/t/unable-to-retrieve-custom-roles-claim-from-access-token-at-userinfo-endpoint/111743>\
**Category:** Get Help\
**Tags:** extensibility, actions-extensibility\
**Created:** [July 20, 2023, 11:42pm UTC](https://community.auth0.com/t/unable-to-retrieve-custom-roles-claim-from-access-token-at-userinfo-endpoint/111743 "2023-07-20T23:42:29Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alex0](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/alex0/32/27029_2.png) [@Alex0](https://community.auth0.com/u/Alex0)\
**Post date:** [July 20, 2023, 11:42pm UTC](https://community.auth0.com/t/unable-to-retrieve-custom-roles-claim-from-access-token-at-userinfo-endpoint/111743/1 "2023-07-20T23:42:29Z")

</div>

Hello Auth0 Community,

I am having trouble retrieving custom “roles” claims from an access token using the /userinfo endpoint. I’ve set up an Auth0 Identity Provider (IdP) and Service Provider (SP), where the SP redirects a user to the IdP for authentication. Upon authentication, the user is redirected back to my application, and both access and ID tokens are generated successfully.

I’ve added custom “roles” claims to these tokens, expecting to retrieve them through the /userinfo endpoint. In my IdP, I’ve added the following custom login action:

```auto
exports.onExecutePostLogin = async (event, api) => {
  const namespace = 'abcnamespace';
  api.idToken.setCustomClaim(`${namespace}/role`, event?.authorization?.roles);
  api.accessToken.setCustomClaim(`${namespace}/role`, event?.authorization?.roles);

  api.idToken.setCustomClaim(`${namespace}/testdata`, "123");
  api.accessToken.setCustomClaim(`${namespace}/testdata`, "123");

  if (event?.user?.user_metadata?.first_name) {
      api.idToken.setCustomClaim(`${namespace}/given_name`, event.user.user_metadata.first_name);
      api.accessToken.setCustomClaim(`${namespace}/given_name`, event.user.user_metadata.first_name);
  }
  if (event?.user?.user_metadata?.last_name) {
      api.idToken.setCustomClaim(`${namespace}/family_name`, event.user.user_metadata.last_name);
      api.accessToken.setCustomClaim(`${namespace}/family_name`, event.user.user_metadata.last_name);
  } 
}

```

Here, “family\_name” and “given\_name” are added correctly to the tokens and can be retrieved using the /userinfo endpoint. However, the “role” claims, while they appear in the user’s rawData on the Auth0 SP dashboard (albeit without the namespace), cannot be retrieved from either the access token or /userinfo endpoint.

From the client-side, I pass the audience as follows:

```auto
<Auth0Provider {...providerConfig}
        scope="openid profile email read:current_user update:current_user_metadata"
        audience={`my-audience-here`}
    >
        {children}
</Auth0Provider>

```

I also attempted adding “read:roles read:role\_members” to the scope, but without success.

FYI, I have read almost all the custom claims posts in the auth0 community but nothing seems to work, including this [How to add Roles and Permissions to the ID Token using Actions?](https://community.auth0.com/t/how-to-add-roles-and-permissions-to-the-id-token-using-actions/84506)

I would appreciate any insights or suggestions on how to correctly add and retrieve these custom “role” claims from the access token and the /userinfo endpoint.

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [July 21, 2023, 12:15am UTC](https://community.auth0.com/t/unable-to-retrieve-custom-roles-claim-from-access-token-at-userinfo-endpoint/111743/3 "2023-07-21T00:15:21Z")

</div>

Hello @Alex0 welcome to the community!

Thanks for the detailed description 🙂 Everything code-wise looks fine, so I’m guessing it’s a configuration issue somewhere.

> [@Alex0](#):
>
> while they appear in the user’s rawData on the Auth0 SP dashboard (albeit without the namespace)

Where exactly are you referring to that you can see the user’s associated roles?

Keep us posted!

---

<div class="post-metadata">

**Author:** ![Alex0](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/alex0/32/27029_2.png) [@Alex0](https://community.auth0.com/u/Alex0)\
**Post date:** [July 21, 2023, 12:22am UTC](https://community.auth0.com/t/unable-to-retrieve-custom-roles-claim-from-access-token-at-userinfo-endpoint/111743/4 "2023-07-21T00:22:51Z")

</div>

They appear in the User Management dashboard for my auth0 SP:

 ![Screenshot 2023-07-20 at 5.21.20 PM](https://global.discourse-cdn.com/auth0/original/3X/4/0/409cd6222f42fda1ca33ecd6843e4c2eac8bd578.png)

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [July 21, 2023, 12:27am UTC](https://community.auth0.com/t/unable-to-retrieve-custom-roles-claim-from-access-token-at-userinfo-endpoint/111743/5 "2023-07-21T00:27:20Z")

</div>

Thanks for confirming! How are you assigning roles? Does the user have the same roles in “Roles” section in the same User Management screen?

---

<div class="post-metadata">

**Author:** ![Alex0](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/alex0/32/27029_2.png) [@Alex0](https://community.auth0.com/u/Alex0)\
**Post date:** [July 21, 2023, 12:40am UTC](https://community.auth0.com/t/unable-to-retrieve-custom-roles-claim-from-access-token-at-userinfo-endpoint/111743/6 "2023-07-21T00:40:07Z")

</div>

So I’m assigning the user roles in my IdP using User Management\>Roles, so a user with 2 roles would look like this on my IdP:

 ![Screenshot 2023-07-20 at 5.34.49 PM](https://global.discourse-cdn.com/auth0/original/3X/1/d/1d8eb492e81264542e402eb416fc7d776729330f.png)

The Raw Json Section would not show the role claim in the IdP, However on the SP, the roles would show in the Raw JSON section:  
 ![Screenshot 2023-07-20 at 5.36.04 PM](https://global.discourse-cdn.com/auth0/original/3X/1/0/103c2c04006ce09981a5ab3802bcb009a29b670c.png)

The roles section in the SP would be empty, which I think is expected:

 ![Screenshot 2023-07-20 at 5.35.10 PM](https://global.discourse-cdn.com/auth0/original/3X/8/6/86896abe0a41ba6f5f27cb9e6cc308bf6aa484d8.png)

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [July 25, 2023, 3:37pm UTC](https://community.auth0.com/t/unable-to-retrieve-custom-roles-claim-from-access-token-at-userinfo-endpoint/111743/8 "2023-07-25T15:37:45Z")

</div>

Hi @Alex0,

I’m jumping in for @ty.frith while he’s out.

Make sure you are following the [Create Custom Claims](https://auth0.com/docs/secure/tokens/json-web-tokens/create-custom-claims#namespaced-guidelines) namespace guidelines.

Specifically:

> Use the following guidelines for namespace identifiers:
> 
> - Use any non-Auth0 HTTP or HTTPS URL as a namespace identifier. Auth0 domains cannot be used as namespace identifiers, and include:
> - [auth0.com](http://auth0.com)
> - [webtask.io](http://webtask.io)
> - webtask.run
> 
> - Use a URL that you control as a namespace identifier; this allows you to avoid the risk that someone else is using the same namespace. The namespace URL does not have to point to an actual resource. It is only used as an identifier; it will not be called.
> - Begin the URL with `http://` or `https://`.
> - Create multiple namespaces, as needed.
> 
> Once you have chosen your namespace, append the claim to it to create a namespaced claim, which can be added to a token. For example:
> 
> `http://www.example.com/favorite_color`

---

<div class="post-metadata">

**Author:** ![Alex0](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/alex0/32/27029_2.png) [@Alex0](https://community.auth0.com/u/Alex0)\
**Post date:** [July 25, 2023, 4:12pm UTC](https://community.auth0.com/t/unable-to-retrieve-custom-roles-claim-from-access-token-at-userinfo-endpoint/111743/9 "2023-07-25T16:12:21Z")

</div>

Thanks @dan.woda, In the original post I shared code where I set up my namespace as `const namespace = 'abcnamespace';` still not showing up in the access token. I also followed this guide: [How to add Roles and Permissions to the ID Token using Actions?](https://community.auth0.com/t/how-to-add-roles-and-permissions-to-the-id-token-using-actions/84506) and also this [Create Custom Claims](https://auth0.com/docs/secure/tokens/json-web-tokens/create-namespaced-custom-claims)

Note that neither `https://abcnamespace` nor `abcnamespace` would show up in the access token or userinfo endpoint

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [July 26, 2023, 12:55pm UTC](https://community.auth0.com/t/unable-to-retrieve-custom-roles-claim-from-access-token-at-userinfo-endpoint/111743/10 "2023-07-26T12:55:04Z")

</div>

Can you try `http://www.example.com` and see if that works?

---

<div class="post-metadata">

**Author:** ![Alex0](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/alex0/32/27029_2.png) [@Alex0](https://community.auth0.com/u/Alex0)\
**Post date:** [July 26, 2023, 8:50pm UTC](https://community.auth0.com/t/unable-to-retrieve-custom-roles-claim-from-access-token-at-userinfo-endpoint/111743/11 "2023-07-26T20:50:38Z")

</div>

@dan.woda I tried using [http://www.example.com](http://www.example.com), and that didn’t work either.

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [July 27, 2023, 1:15pm UTC](https://community.auth0.com/t/unable-to-retrieve-custom-roles-claim-from-access-token-at-userinfo-endpoint/111743/12 "2023-07-27T13:15:37Z")

</div>

Okay, I was able to test it and confirm both the original code you posted (with the `abc...` namespace), and with the `http://www.example.com` namespace.

I used that Action and was able to retrieve the custom claims in the ID and Access Tokens and from the userinfo endpoint.

I’m wondering if maybe the Action isn’t firing at all. Can you please confirm it has been deployed _and_ has been added to the post login actions flow? (If this doesn’t solve it, please DM me your tenant name so I can look at your config, thanks.)

 ![image](https://global.discourse-cdn.com/auth0/original/3X/1/5/15be48f5fc6c0a88cc076422d61648161a0f2496.png)

 ![image](https://global.discourse-cdn.com/auth0/original/3X/3/d/3d951edea350fc427c54347d4fd3dfaa975aff3e.png)

---

<div class="post-metadata">

**Author:** ![Alex0](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/alex0/32/27029_2.png) [@Alex0](https://community.auth0.com/u/Alex0)\
**Post date:** [July 27, 2023, 7:42pm UTC](https://community.auth0.com/t/unable-to-retrieve-custom-roles-claim-from-access-token-at-userinfo-endpoint/111743/13 "2023-07-27T19:42:37Z")

</div>

Thank you for looking into this, The action has been deployed and apparently is triggering as I can see the custom claim (although without the namespace) returned in the raw user object in the SP, I uploaded some screenshots a few days ago.

I’ve DM’ed you, my tenant name.

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [July 28, 2023, 1:36pm UTC](https://community.auth0.com/t/unable-to-retrieve-custom-roles-claim-from-access-token-at-userinfo-endpoint/111743/14 "2023-07-28T13:36:31Z")

</div>

Perfect, I replied in the DM.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [August 23, 2023, 2:40pm UTC](https://community.auth0.com/t/unable-to-retrieve-custom-roles-claim-from-access-token-at-userinfo-endpoint/111743/15 "2023-08-23T14:40:47Z")

</div>

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