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

Hi, I’m trying to return user role as part of the IdToken custom claim. According to the documentation I tried to use one of the template RULE, Add user roles from a SQL Server database. And when I tried to log in and get id token, I got this error:

The “config.server” property is required and must be of type string.

which seems out of my control? Can you suggest a way to fix it?

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.

Ok thanks for the quick reply, I tried the snippet and it works. But I only see the roles in id token is there a way to include also the organizations that the user belongs to and the corresponding permission info in the id token too? Thanks

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