# I am unable to add the role which was added during the first login to the access token

**URL:** <https://community.auth0.com/t/i-am-unable-to-add-the-role-which-was-added-during-the-first-login-to-the-access-token/112270>\
**Category:** Get Help\
**Tags:** management-api, roles\
**Created:** [July 27, 2023, 2:42pm UTC](https://community.auth0.com/t/i-am-unable-to-add-the-role-which-was-added-during-the-first-login-to-the-access-token/112270 "2023-07-27T14:42:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![shester](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/shester/32/47464_2.png) [@shester](https://community.auth0.com/u/shester)\
**Post date:** [July 27, 2023, 2:42pm UTC](https://community.auth0.com/t/i-am-unable-to-add-the-role-which-was-added-during-the-first-login-to-the-access-token/112270/1 "2023-07-27T14:42:10Z")

</div>

I have the following actions in my Login flow:

1. Assign Role On First Login:

```auto
exports.onExecutePostLogin = async (event, api) => {
  if (event.stats.logins_count !== 1) {
    return;
  }

  const ManagementClient = require('auth0').ManagementClient;

  const management = new ManagementClient({
      domain: event.secrets.domain,
      clientId: event.secrets.clientId,
      clientSecret: event.secrets.clientSecret
  });

  const params = { id : event.user.user_id};
  const data = { "roles" : ["rol_7OQ4PJQWyE0R0eQC"]};

  try {
    const res = await management.assignRolestoUser(params, data)
  } catch (e) {
    console.log(e)
    // Handle error
  }
};

```

1. Enrich Access Token

```auto
exports.onExecutePostLogin = async (event, api) => {
  if (event.authorization){
    api.accessToken.setCustomClaim("user_id", event.user.user_id);
    api.accessToken.setCustomClaim("user_name", event.user.name);
    api.accessToken.setCustomClaim("user_email", event.user.email);

    const hasuraClaims = {
      "x-hasura-user-id": event.user.user_id
    };

    let roles;
    if (event.stats.logins_count === 1) {
      const ManagementClient = require('auth0').ManagementClient;

      const management = new ManagementClient({
          domain: event.secrets.domain,
          clientId: event.secrets.clientId,
          clientSecret: event.secrets.clientSecret
      });

      const params = { id : event.user.user_id};

      try {
        roles = await management.getUserRoles(params)
      } catch (e) {
        console.log(e)
        // Handle error
      }
    }
    else {
      roles = event.authorization.roles.map(role => role.toLowerCase());
    }
    
    if (roles && roles.length > 0) {
      api.idToken.setCustomClaim("user_roles", roles);
      api.accessToken.setCustomClaim("user_roles", roles);

      const defaultRole = roles.reduce((highestRole, currentRole) => {
        if (currentRole === "admin") {
          return currentRole;
        }
        if (currentRole === "manager" && highestRole !== "admin") {
          return currentRole;
        }
        if (currentRole === "editor" && highestRole !== "admin" && highestRole !== "manager") {
          return currentRole;
        }
        if (currentRole === "viewer" && highestRole !== "admin" && highestRole !== "manager" && highestRole !== "editor") {
          return currentRole;
        }
        return highestRole;
      }, "");

      hasuraClaims["x-hasura-default-role"] = defaultRole;
      hasuraClaims["x-hasura-allowed-roles"] = roles;
    }
    
    api.accessToken.setCustomClaim("https://hasura.io/jwt/claims", hasuraClaims);
  }
};

```

The first action successfully adds the role on the first login. However, in the second action during the first login, `event.authorization.roles` does not contain the role that was added in the previous action. To get around this, I tried to use the management API to `getUserRoles`, but this does not seem to be working as expected.

What is the recommended approach here? Is there some way to add the added role to the event from the first action so it can be consumed normally by the second action? Should I be using the management API in the second action to get the user roles (like I’m currently trying to do, but in the correct way)?

Please advise. Thanks!

---

<div class="post-metadata">

**Author:** ![shester](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/shester/32/47464_2.png) [@shester](https://community.auth0.com/u/shester)\
**Post date:** [July 27, 2023, 3:01pm UTC](https://community.auth0.com/t/i-am-unable-to-add-the-role-which-was-added-during-the-first-login-to-the-access-token/112270/2 "2023-07-27T15:01:50Z")

</div>

Disregard… I figured it out. The issue is that the Machine to Machine application I am using to connect to the Management API needed the `read:users` permission (which I didn’t have) along with the `read:roles` permission (which I did have). It also needs the `update:roles` permission to be able to set the role from the first action. Here is the updated second action:

```auto
exports.onExecutePostLogin = async (event, api) => {
  if (event.authorization){
    api.accessToken.setCustomClaim("user_id", event.user.user_id);
    api.accessToken.setCustomClaim("user_name", event.user.name);
    api.accessToken.setCustomClaim("user_email", event.user.email);

    const hasuraClaims = {
      "x-hasura-user-id": event.user.user_id
    };

    let roles;
    if (event.stats.logins_count === 1) {
      const ManagementClient = require('auth0').ManagementClient;

      const management = new ManagementClient({
          domain: event.secrets.domain,
          clientId: event.secrets.clientId,
          clientSecret: event.secrets.clientSecret
      });

      const params = { id : event.user.user_id};

      try {
        const userRoles = await management.getUserRoles(params);
        roles = userRoles.map((role) => role.name.toLowerCase());
      } catch (e) {
        console.log(e);
        // Handle error
      }
    }
    else {
      roles = event.authorization.roles.map(role => role.toLowerCase());
    }
    
    if (roles && roles.length > 0) {
      api.idToken.setCustomClaim("user_roles", roles);
      api.accessToken.setCustomClaim("user_roles", roles);

      const defaultRole = roles.reduce((highestRole, currentRole) => {
        if (currentRole === "admin") {
          return currentRole;
        }
        if (currentRole === "manager" && highestRole !== "admin") {
          return currentRole;
        }
        if (currentRole === "editor" && highestRole !== "admin" && highestRole !== "manager") {
          return currentRole;
        }
        if (currentRole === "viewer" && highestRole !== "admin" && highestRole !== "manager" && highestRole !== "editor") {
          return currentRole;
        }
        return highestRole;
      }, "");

      hasuraClaims["x-hasura-default-role"] = defaultRole;
      hasuraClaims["x-hasura-allowed-roles"] = roles;
    }
    
    api.accessToken.setCustomClaim("https://hasura.io/jwt/claims", hasuraClaims);
  }
};

```

---

<div class="post-metadata">

**Author:** ![pityjonson4](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/pityjonson4/32/54138_2.png) [@pityjonson4](https://community.auth0.com/u/pityjonson4)\
**Post date:** [June 12, 2024, 2:00pm UTC](https://community.auth0.com/t/i-am-unable-to-add-the-role-which-was-added-during-the-first-login-to-the-access-token/112270/3 "2024-06-12T14:00:05Z")

</div>

I am unable to add the role which was added during the first login to the access token[.](https://sssmidportal.com/) This issue suggests a challenge in integrating newly assigned roles into existing access tokens effectively. Addressing this requires reevaluating the token generation process to ensure it dynamically reflects updated user roles for secure and efficient access management.

---

<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:** [June 26, 2024, 2:00pm UTC](https://community.auth0.com/t/i-am-unable-to-add-the-role-which-was-added-during-the-first-login-to-the-access-token/112270/4 "2024-06-26T14:00:30Z")

</div>

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