# Adding user roles to access token

**URL:** <https://community.auth0.com/t/adding-user-roles-to-access-token/97373>\
**Category:** Get Help\
**Tags:** management-api, roles\
**Created:** [January 3, 2023, 9:10am UTC](https://community.auth0.com/t/adding-user-roles-to-access-token/97373 "2023-01-03T09:10:18Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![chris.tice](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/chris.tice/32/42255_2.png) [@chris.tice](https://community.auth0.com/u/chris.tice)\
**Post date:** [January 5, 2023, 6:27am UTC](https://community.auth0.com/t/adding-user-roles-to-access-token/97373/4 "2023-01-05T06:27:20Z")

</div>

What I ended up doing is using a custom Action with the Management API to query for a users direct roles, and adding the list of roles to the token as a custom claim.

> **[Login Flow](https://auth0.com/docs/customize/actions/flows-and-triggers/login-flow)**
>
> Learn about the Actions Login Flow and the post-login Action trigger, which is executed after a user logs in and when a Refresh Token is requested.

```auto
exports.onExecutePostLogin = async (event, api) => {
  const ManagementClient = require('auth0').ManagementClient;
  const management = new ManagementClient({
      domain: event.secrets.domain,
      clientId: event.secrets.clientId,
      clientSecret: event.secrets.clientSecret,
  });

  const namespace = 'my-namespace';
  try {
    // Get the list of global roles for a user
    const roles = await management.getUserRoles({
      id: event.user.user_id,
    });

    const roleNames = roles.map((role) => role.name);

    api.idToken.setCustomClaim(`${namespace}/globalRoles`, roleNames);
    api.accessToken.setCustomClaim(`${namespace}/globalRoles`, roleNames);
  } catch (e) {
    console.log(e);
  }
};

```

---

_[View the full topic](https://community.auth0.com/t/adding-user-roles-to-access-token/97373)._
