# How do I attach the roles the user has assigned to the ID token using Actions?

**URL:** <https://community.auth0.com/t/how-do-i-attach-the-roles-the-user-has-assigned-to-the-id-token-using-actions/185418>\
**Category:** Get Help\
**Tags:** management-api, roles\
**Created:** [April 13, 2025, 4:39pm UTC](https://community.auth0.com/t/how-do-i-attach-the-roles-the-user-has-assigned-to-the-id-token-using-actions/185418 "2025-04-13T16:39:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jackiegleason](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/jackiegleason/32/8119_2.png) [@jackiegleason](https://community.auth0.com/u/jackiegleason)\
**Post date:** [April 13, 2025, 4:39pm UTC](https://community.auth0.com/t/how-do-i-attach-the-roles-the-user-has-assigned-to-the-id-token-using-actions/185418/1 "2025-04-13T16:39:15Z")

</div>

I am trying to add the roles the users have in the ID Token. All of the responses on here seem pretty old so they are all rule related. I tried this

```auto
exports.onExecutePostLogin = async (event, api) => {
    if(event.authorization != null){
        api.idToken.setCustomClaim("roles", event.authorization.roles)
    }
};

```

But it didn’t work I looked at the template and it uses

```auto
const specialName = event.secrets.SPECIAL_ROLE_NAME;
 const specialValues = event.secrets.SPECIAL_ROLE_VALUE?.split(',').map(
        (v) => v.trim()
    );

```

But I am not sure what I need to do to set the `SPECIAL_ROLE_*`

How do I handle this?

---

<div class="post-metadata">

**Author:** ![nik.baleca](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/nik.baleca/32/68749_2.png) [@nik.baleca](https://community.auth0.com/u/nik.baleca)\
**Post date:** [April 14, 2025, 6:01pm UTC](https://community.auth0.com/t/how-do-i-attach-the-roles-the-user-has-assigned-to-the-id-token-using-actions/185418/3 "2025-04-14T18:01:44Z")

</div>

Hi @jackiegleason

The first action code you have posted should work fine in setting a custom claim including the user roles, however, I believe the issue resides in the fact that you cannot use just `roles` for the namespace since it is part of the general restrictions list mentioned in our [documentation](https://auth0.com/docs/secure/tokens/json-web-tokens/create-custom-claims#general-restrictions).

I have used the following action code and I was able to retrieve the roles inside the `IdToken`:

```auto
exports.onExecutePostLogin = async (event, api) => {

  api.idToken.setCustomClaim("https://randomnamespace.net/roles", event.authorization.roles);

};

```

Resulting IdToken:

```auto
{
  "sub": "{{user.id}}",
  "given_name": "Jack",
  "family_name": "Daniels",
  "nickname": "{{user.nickname}}",
  "name": "{{user.email}}",
  "picture": "somerandompic.png",
  "updated_at": "2025-04-14T17:57:30.657Z",
  "https://randomnamespace.net/roles": [
    "Secondary Test",
    "Test"
  ]
}

```

If you have any other questions, feel free to leave a reply!

Kind Regards,  
Nik

---

<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:** [April 28, 2025, 6:02pm UTC](https://community.auth0.com/t/how-do-i-attach-the-roles-the-user-has-assigned-to-the-id-token-using-actions/185418/4 "2025-04-28T18:02:11Z")

</div>

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