# Why is roles claim missing after signup?

**URL:** <https://community.auth0.com/t/why-is-roles-claim-missing-after-signup/65953>\
**Category:** Get Help\
**Tags:** signup, rules\
**Created:** [July 11, 2021, 12:04pm UTC](https://community.auth0.com/t/why-is-roles-claim-missing-after-signup/65953 "2021-07-11T12:04:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![erlend](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/erlend/32/29987_2.png) [@erlend](https://community.auth0.com/u/erlend)\
**Post date:** [July 11, 2021, 12:04pm UTC](https://community.auth0.com/t/why-is-roles-claim-missing-after-signup/65953/1 "2021-07-11T12:04:55Z")

</div>

As a part of the user signup I’ve added a rule to set the user role based on a signup query parameter.  
When I inspect the users profile after signing up, the role is applied.  
My problem is that the token I receive back from the signup process, does not contain the newly assigned role of the user. I explicitly to try set the roles claim of the token to a known value, but when I inspect the token, the roles claim is missing. Somehow it’s being overwritten by something. If the user logs out and back in again, the token has the roles claim set as expected.

This is the rule I run after the rule where I apply the role:

```auto
function (user, context, callback) {
  
  const namespace = 'mynamespace';
  const assignedRoles = context.authorization || {}.roles;
  user.user_metadata = user.user_metadata || {};
  user.app_metadata = user.app_metadata || {};
  let accessTokenClaims = context.accessToken || {};
  
  if (user.app_metadata.mode === "SuperCoolUser") {
    console.log("I am supercool user");
    accessTokenClaims[`${namespace}/roles`] = user.app_metadata.mode;
  }
  
  accessTokenClaims[`${namespace}/userId`] = user.user_id;
  accessTokenClaims[`${namespace}/user`] = user.user_metadata.user;
  accessTokenClaims[`${namespace}/mode`] = user.app_metadata.mode;
  
  context.accessToken = accessTokenClaims;
  return callback(null, user, context);
}

```

How can I work around this?

---

<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:** [July 12, 2021, 7:25pm UTC](https://community.auth0.com/t/why-is-roles-claim-missing-after-signup/65953/3 "2021-07-12T19:25:04Z")

</div>

Hi @erlend,

Can you confirm that there isn’t another rule that is writing over the accessToken object?

---

<div class="post-metadata">

**Author:** ![erlend](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/erlend/32/29987_2.png) [@erlend](https://community.auth0.com/u/erlend)\
**Post date:** [July 14, 2021, 6:21pm UTC](https://community.auth0.com/t/why-is-roles-claim-missing-after-signup/65953/4 "2021-07-14T18:21:33Z")

</div>

Hi @dan.woda, here is the summary of rules and flows.

```auto
1 (rule).

function(user, context, callback) {
    Promise.resolve()
      .then(() => {
        var response = {};
        response.user = user;
        user.app_metadata.mode = context.request.query.mode;
        var role = context.request.query.mode;
        auth0.users.updateAppMetadata(user.user_id, user.app_metadata);

        // only set the role from the query param first time.
        if (role === 'SuperCoolUser') {
          var count = context.stats && context.stats.loginsCount ? context.stats.loginsCount : 0;
          if (count > 1) {
            return callback(null, user, context);
          }
  
          var ManagementClient = require('auth0@2.17.0').ManagementClient;
          var management = new ManagementClient({
            token: auth0.accessToken,
            domain: auth0.domain
          });
  
          // assign the role from the query param to the user.
          management.assignRolestoUser(
              {id: user.user_id}, {"roles": ["role_id"]
            }, 
            (err) => {
                console.log("Failed to assign roles: " + err);
              if (!err) {
                console.log("Successfully assigned roles");
              }
              return callback(err, user, context);
            });
        }
        return callback(null, user, context);
      })
      .then(() => {
        callback(null, user, context);
      })
      .catch(reason => {
        console.log(reason);
        callback(reason);
      });
  }

2 (rule). function (user, context, callback) {
  
  const namespace = 'mynamespace';
  const assignedRoles = context.authorization || {}.roles;
  user.user_metadata = user.user_metadata || {};
  user.app_metadata = user.app_metadata || {};
  let accessTokenClaims = context.accessToken || {};
  
  if (user.app_metadata.mode === "SuperCoolUser") {
    console.log("I am supercool user");
    accessTokenClaims[`${namespace}/roles`] = user.app_metadata.mode;
  }
  
  accessTokenClaims[`${namespace}/userId`] = user.user_id;
  accessTokenClaims[`${namespace}/user`] = user.user_metadata.user;
  accessTokenClaims[`${namespace}/mode`] = user.app_metadata.mode;
  
  context.accessToken = accessTokenClaims;
  return callback(null, user, context);
}

3 (flow).

exports.onExecutePostLogin = async (event, api) => {
  const namespace = 'https://mysupercoolnamespace';
  if (event.authorization) {
    api.idToken.setCustomClaim(`${namespace}/roles`, event.authorization.roles);
    api.accessToken.setCustomClaim(`${namespace}/roles`, event.authorization.roles);
    api.accessToken.setCustomClaim(`${namespace}/userId`, event.user.email);
    api.accessToken.setCustomClaim(`${namespace}/user`, event.user.user_metadata.user);
  }
}

```

I’m not sure if the login flow is executed before returning the token to the user?  
Regards,

Erlend

---

<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:** [July 14, 2021, 10:44pm UTC](https://community.auth0.com/t/why-is-roles-claim-missing-after-signup/65953/5 "2021-07-14T22:44:10Z")

</div>

I think you may be missing the role on first login because the context.authorization object is already created before you have added the role to the user. You will have to manually add the role to the token on first login in addition to adding it via the management API.

---

<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:** [August 23, 2021, 4:20pm UTC](https://community.auth0.com/t/why-is-roles-claim-missing-after-signup/65953/6 "2021-08-23T16:20:47Z")

</div>

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