# Create custom claim on role level

**URL:** <https://community.auth0.com/t/create-custom-claim-on-role-level/138800>\
**Category:** Get Help\
**Tags:** user-profile, user-management\
**Created:** [July 13, 2024, 1:15pm UTC](https://community.auth0.com/t/create-custom-claim-on-role-level/138800 "2024-07-13T13:15:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jochen.walz](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@jochen.walz](https://community.auth0.com/u/jochen.walz)\
**Post date:** [July 13, 2024, 1:15pm UTC](https://community.auth0.com/t/create-custom-claim-on-role-level/138800/1 "2024-07-13T13:15:01Z")

</div>

Is it possible to create a custom claim included in the access token for all users who have a specific role assigned? I.e., I’d not like to define the content of the claim for individual users who have that role (because the claim content is identical for all these users), but for the role. Then add it to the access token in a post-login action.

Is it possible to realize such thing with the Authentication Core (without the extension)?

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [July 15, 2024, 7:44pm UTC](https://community.auth0.com/t/create-custom-claim-on-role-level/138800/3 "2024-07-15T19:44:57Z")

</div>

Hi @jochen.walz,

Yes, it is possible to add a custom claim to the access token if they have a specific role assigned.

Something like the following could work:

```auto
exports.onExecutePostLogin = async (event, api) => {
const namespace = 'https://myapp.example.com';
if (event.authorization.roles.includes('admin'))) {
    // Set claims in access token
    api.accessToken.setCustomClaim(`${namespace}/role`, "admin");
  }
};

```

Reference: [Adding custom claims to tokens](https://community.auth0.com/t/adding-custom-claims-to-tokens/84590)

Let me know if there’s anything else I can do to help.

Thanks,  
Rueben

---

<div class="post-metadata">

**Author:** ![jochen.walz](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@jochen.walz](https://community.auth0.com/u/jochen.walz)\
**Post date:** [July 16, 2024, 3:06pm UTC](https://community.auth0.com/t/create-custom-claim-on-role-level/138800/4 "2024-07-16T15:06:52Z")

</div>

Thanks Rueben!

Depending on how much granularity we must provide, we may end up with a longer list of if-else-if-else-… (or a switch). I had hoped for some option to define the content of the claim on role level which I just haven’t found yet and have a one-liner to add it to the token in the action. But obviously that’s what it is.

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [July 17, 2024, 6:57pm UTC](https://community.auth0.com/t/create-custom-claim-on-role-level/138800/5 "2024-07-17T18:57:55Z")

</div>

Hi @jochen.walz,

Yes, that would work with a longer list of if-else statements or a switch.

Unfortunately, there isn’t an option to define the content at the role level. Only permissions can be defined at the Role level. Perhaps you could leverage that if it works for your use case.

If you decide to use permissions, you can toggle on a feature in your API settings to **Add Permissions in the Access Token**.

I hope this helps!

Let me know if you have any further questions.

Cheers,  
Rueben

---

<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:** [July 31, 2024, 6:58pm UTC](https://community.auth0.com/t/create-custom-claim-on-role-level/138800/6 "2024-07-31T18:58:32Z")

</div>

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