# Post login action - trasnfer user role to the backend

**URL:** <https://community.auth0.com/t/post-login-action-trasnfer-user-role-to-the-backend/113001>\
**Category:** Get Help\
**Tags:** user-profile, user-management\
**Created:** [August 7, 2023, 5:37am UTC](https://community.auth0.com/t/post-login-action-trasnfer-user-role-to-the-backend/113001 "2023-08-07T05:37:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![almogco94](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/almogco94/32/48638_2.png) [@almogco94](https://community.auth0.com/u/almogco94)\
**Post date:** [August 7, 2023, 5:37am UTC](https://community.auth0.com/t/post-login-action-trasnfer-user-role-to-the-backend/113001/1 "2023-08-07T05:37:23Z")

</div>

Hey im using post login action to get my user role from user mangement api and i want to transfer this role in the access toke that could get it in my express backend in req.oidc.user .  
Im using express-openid-connect on my backend but seems like i dont get any role on the backend.  
any idea how i can make it work?

```auto
exports.onExecutePostLogin = async (event, api) => {
  const ManagementClient = require('auth0').ManagementClient;
  const management = new ManagementClient({
      domain: event.secrets.domain, 
      clientId: event.secrets.clientId,
      clientSecret: event.secrets.clientSecret,
  });

  const namespace = 'http://localhost:3000';
  try {
    // Get the list of global roles for a user
    const roles = await management.getUserRoles({
      id: event.user.user_id,
    });
    const roleNames = roles.map((role) => role.name); 

    api.idToken.setCustomClaim(`${namespace}/roles`, roleNames);
    api.accessToken.setCustomClaim(`${namespace}/roles`, roleNames);
  

  } catch (e) {
    console.log(e);
  }
};

```

---

<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:** [August 10, 2023, 8:38pm UTC](https://community.auth0.com/t/post-login-action-trasnfer-user-role-to-the-backend/113001/3 "2023-08-10T20:38:10Z")

</div>

Hi @almogco94,

Welcome to the Auth0 Community!

You will want to avoid calling the management API every time a user authenticates, as you will quickly run into the rate limit.

Try this code instead:

```javascript
/**
 * @param {Event} event - Details about the user and the context in which they are logging in.
 * @param {PostLoginAPI} api - Interface whose methods can be used to change the behavior of the login.
 */
exports.onExecutePostLogin = async (event, api) => {
  const namespace = 'https://my-app.example.com';
  if (event.authorization) {
    api.idToken.setCustomClaim(`${namespace}/roles`, event.authorization.roles);
    api.accessToken.setCustomClaim(`${namespace}/roles`, event.authorization.roles);
  }
}

```

If this doesn’t solve it, can you share how you are trying to access the roles in your backend?

---

<div class="post-metadata">

**Author:** ![almogco94](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/almogco94/32/48638_2.png) [@almogco94](https://community.auth0.com/u/almogco94)\
**Post date:** [August 14, 2023, 2:45pm UTC](https://community.auth0.com/t/post-login-action-trasnfer-user-role-to-the-backend/113001/4 "2023-08-14T14:45:18Z")

</div>

Hey thanks for help, i did resolve the backend by using mangement api library of auth0.  
And this is my new action of post login those this make more sense now ?

```auto
/**
* @param {Event} event - Details about the user and the context in which they are logging in.

* @param {PostLoginAPI} api - Interface whose methods can be used to change the behavior of the login.
*/
exports.onExecutePostLogin = async (event, api) => {
  if (event.authorization) {
  const namespace = 'http://localhost:3000';

    if(event.stats.logins_count === 1) {

    const ManagementClient = require("auth0").ManagementClient;
      // Get the user ID from the event data
    const userId = event.user.user_id;
    const user = "rol_SIxIDjSx9yJs0mbZ";
    // Initialize the ManagementClient
    const management = new ManagementClient({
      domain: 'dev-yr71h8521c4gjrf0.us.auth0.com', // Update this to your actual Auth0 domain in production
      clientId: 'Q9eh4TulIhNL9nCe1bKlOuaCxbXNnRr9',
      clientSecret: 'Dv5vdJPseW--0LudpyiqhBkuvnfFqfqzv8jRThGbNA8iXXVXmjfGGVAURL4zZGtv',
    });
   // Assign the desired role(s) to the user
    api.idToken.setCustomClaim(`${namespace}/roles`, ["user"]);
    api.accessToken.setCustomClaim(`${namespace}/roles`, ["user"]);
    await management.assignRolestoUser({ id: userId }, { roles: [user] });
    return;
    }
    
    api.idToken.setCustomClaim(`${namespace}/roles`, event.authorization.roles);
    api.accessToken.setCustomClaim(`${namespace}/roles`, event.authorization.roles);
  }
}

```

---

<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 29, 2023, 1:05pm UTC](https://community.auth0.com/t/post-login-action-trasnfer-user-role-to-the-backend/113001/5 "2023-08-29T13:05:24Z")

</div>

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