# Add roles to access token

**URL:** <https://community.auth0.com/t/add-roles-to-access-token/76471>\
**Category:** Get Help\
**Tags:** rules, access-token, auth0, roles\
**Created:** [January 15, 2022, 6:00pm UTC](https://community.auth0.com/t/add-roles-to-access-token/76471 "2022-01-15T18:00:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![giovannilaperna](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/giovannilaperna/32/34414_2.png) [@giovannilaperna](https://community.auth0.com/u/giovannilaperna)\
**Post date:** [January 15, 2022, 6:00pm UTC](https://community.auth0.com/t/add-roles-to-access-token/76471/1 "2022-01-15T18:00:59Z")

</div>

This has been a recurring topic, however none of the proposed solution worked for me.

1. I created a role
2. I assigned the role to my user.
3. I created a custom rule as suggested [here](https://community.auth0.com/t/add-roles-to-the-access-token/32140/5):

```auto
function addEmailToAccessToken(user, context, callback) {
  // This rule adds the authenticated user's roles to the access token.

  var namespace = 'https://my-url.us.auth0.com/';

  context.accessToken[namespace + 'roles'] = user.app_metadata.roles;
  return callback(null, user, context);
}

```

However `user.app_metadata` is `undefined`  
Which should be the correct namespace?

---

<div class="post-metadata">

**Author:** ![giovannilaperna](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/giovannilaperna/32/34414_2.png) [@giovannilaperna](https://community.auth0.com/u/giovannilaperna)\
**Post date:** [January 16, 2022, 5:09am UTC](https://community.auth0.com/t/add-roles-to-access-token/76471/2 "2022-01-16T05:09:03Z")

</div>

Thanks to [this](https://community.auth0.com/t/add-authorization-core-role-to-user-object/65071/8) topic I found the solution:

```auto
function (user, context, callback) {
    // This rule adds the authenticated user's roles to the access token.

  var namespace = 'https://my-domain.com/';
  context.accessToken[namespace + 'roles'] = context.authorization.roles;
  callback(null, user, context);
}

```

---

<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:** [January 18, 2022, 2:21pm UTC](https://community.auth0.com/t/add-roles-to-access-token/76471/3 "2022-01-18T14:21:37Z")

</div>

Thanks for posting an update @giovannilaperna!

---

<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:** [February 2, 2022, 2:22pm UTC](https://community.auth0.com/t/add-roles-to-access-token/76471/4 "2022-02-02T14:22:36Z")

</div>

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