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

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 to accomplish your preferred use case.

For convenience, the code snippet is:

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.