# Can't use rule to include user role in id token

**URL:** <https://community.auth0.com/t/cant-use-rule-to-include-user-role-in-id-token/73460>\
**Category:** Get Help\
**Created:** [November 19, 2021, 5:15pm UTC](https://community.auth0.com/t/cant-use-rule-to-include-user-role-in-id-token/73460 "2021-11-19T17:15:39Z")\
**Posts on this page:** 1\
**Showing post:** 2

<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:** [November 19, 2021, 7:30pm UTC](https://community.auth0.com/t/cant-use-rule-to-include-user-role-in-id-token/73460/2 "2021-11-19T19:30:24Z")

</div>

Hi @clin,

Welcome to the Auth0 Community!

I understand that you are trying to use a Rule to add the user’s role in the ID Token.

First, I believe that the _Add user roles from a SQL Server database_ is not the Rule you are looking for to add user roles to the ID Token.

Instead, you’ll need to use the [Add user roles to tokens rule](https://auth0.com/docs/authorization/authorization-policies/sample-use-cases-rules-with-authorization#add-user-roles-to-tokens) to accomplish your preferred use case.

For convenience, the code snippet is:

```auto
function (user, context, callback) {
  const namespace = 'http://demozero.net';
  const assignedRoles = (context.authorization || {}).roles;

  let idTokenClaims = context.idToken || {};
  let accessTokenClaims = context.accessToken || {};

  idTokenClaims[`${namespace}/roles`] = assignedRoles;
  accessTokenClaims[`${namespace}/roles`] = assignedRoles;

  context.idToken = idTokenClaims;
  context.accessToken = accessTokenClaims;

  callback(null, user, context);
}

```

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

Thank you.

---

_[View the full topic](https://community.auth0.com/t/cant-use-rule-to-include-user-role-in-id-token/73460)._
