# What is the best way of retrieving user roles?

**URL:** <https://community.auth0.com/t/what-is-the-best-way-of-retrieving-user-roles/88579>\
**Category:** Get Help\
**Tags:** management-api, auth0\
**Created:** [August 11, 2022, 1:46pm UTC](https://community.auth0.com/t/what-is-the-best-way-of-retrieving-user-roles/88579 "2022-08-11T13:46:20Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![lyubomir.nikov](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/lyubomir.nikov/32/36625_2.png) [@lyubomir.nikov](https://community.auth0.com/u/lyubomir.nikov)\
**Post date:** [August 11, 2022, 1:46pm UTC](https://community.auth0.com/t/what-is-the-best-way-of-retrieving-user-roles/88579/1 "2022-08-11T13:46:20Z")

</div>

Hello!

I am building admin portal where I have to manage auth0 users. Today I ran into an exception (429 too many requests) with the rate limiter on that api “https://AUTH\_DOMAIN/api/v2/users/USER\_ID/roles”

Current flow of my backend for retrieving users with their roles:

1. Get all users
2. Retrieve role for each of the users (a request to “https://AUTH\_DOMAIN/api/v2/users/USER\_ID/roles” for each of the users to get his role)
3. Return list of users to UI

Since I have more than 10 users already (and the limit for this endpoint is 10 requests) I started getting errors.

What is the best way to retrieve those roles without actually calling Management API for each user to get the role? Is there a way I can bind the user roles to the metadata with a rule for instance?

Any feedback will be appreciated.  
Thanks,  
Lyubomir

---

<div class="post-metadata">

**Author:** ![SaqibHussain](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/saqibhussain/32/42924_2.png) [@SaqibHussain](https://community.auth0.com/u/SaqibHussain)\
**Post date:** [August 12, 2022, 8:15am UTC](https://community.auth0.com/t/what-is-the-best-way-of-retrieving-user-roles/88579/2 "2022-08-12T08:15:40Z")

</div>

Hi @lyubomir.nikov

Thanks for getting in touch with us at Auth0 Community.

You can add the user roles to the app\_metadata of a user account via a Post Login Action. The simplest implementation of this would be as below:

```auto
exports.onExecutePostLogin = async (event, api) => {
  var roles = event.authorization.roles;
  api.user.setAppMetadata("roles", roles);
};

```

You can read a bit more about this here [https://auth0.com/docs/manage-users/user-accounts/metadata/manage-user-metadata](https://auth0.com/docs/manage-users/user-accounts/metadata/manage-user-metadata)

I hope this helps.  
Warm regards.

---

<div class="post-metadata">

**Author:** ![lyubomir.nikov](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/lyubomir.nikov/32/36625_2.png) [@lyubomir.nikov](https://community.auth0.com/u/lyubomir.nikov)\
**Post date:** [August 12, 2022, 8:30am UTC](https://community.auth0.com/t/what-is-the-best-way-of-retrieving-user-roles/88579/3 "2022-08-12T08:30:35Z")

</div>

Hello @SaqibHussain,

Thanks for your response!

As an admin for the application I would like to get the roles for each of the users registered for the application - therefore I am not interested in obtaining the role on login.

Regards

---

<div class="post-metadata">

**Author:** ![SaqibHussain](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/saqibhussain/32/42924_2.png) [@SaqibHussain](https://community.auth0.com/u/SaqibHussain)\
**Post date:** [August 12, 2022, 8:59am UTC](https://community.auth0.com/t/what-is-the-best-way-of-retrieving-user-roles/88579/4 "2022-08-12T08:59:44Z")

</div>

Hi @lyubomir.nikov

With this Action in place, when a user logs in, their roles will bind to the app\_metadata on that user account which is what I think you were trying to achieve. So when you “Get all users” in your point 1, if the app\_metadata is also being returned then the roles will also be returned and you won’t need to use “https://AUTH\_DOMAIN/api/v2/users/USER\_ID/roles”

Tradeoff here though is that you need to wait for all users to login at least once for the app\_metadata to populate.

Warm regards.

---

<div class="post-metadata">

**Author:** ![aaron.hardy](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/aaron.hardy/32/39201_2.png) [@aaron.hardy](https://community.auth0.com/u/aaron.hardy)\
**Post date:** [October 13, 2022, 4:47am UTC](https://community.auth0.com/t/what-is-the-best-way-of-retrieving-user-roles/88579/5 "2022-10-13T04:47:55Z")

</div>

I have the same issue as @lyubomir.nikov. @SaqibHussain , I think you’re misunderstanding what’s being asked.

Think of an application that provides a user administration view where an admin can come in and assign/unassign roles to users and perform other user management tasks. Naturally, this administration view would show a list of users. Alongside the list of users, we want to show each user’s roles. If this view shows the roles for the displayed users based on what’s in those users’ metadata, it will show out-of-date roles if any of those users have not logged in since the time the admin last changed their roles.

We need a way to get **up-to-date** roles for a list of users. Because the roles must be up to date, we can’t rely on a post-login action.

---

<div class="post-metadata">

**Author:** ![SaqibHussain](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/saqibhussain/32/42924_2.png) [@SaqibHussain](https://community.auth0.com/u/SaqibHussain)\
**Post date:** [October 16, 2022, 2:36pm UTC](https://community.auth0.com/t/what-is-the-best-way-of-retrieving-user-roles/88579/6 "2022-10-16T14:36:50Z")

</div>

Hi @aaron.hardy  
Thanks for your feedback.  
To get the most up to date information you would have to use the Management API [https://auth0.com/docs/api/management/v2#!/Users/get\_user\_roles](https://auth0.com/docs/api/management/v2#!/Users/get_user_roles)

If you’re hitting [rate limits](https://auth0.com/docs/troubleshoot/customer-support/operational-policies/rate-limit-policy/management-api-endpoint-rate-limits) as @lyubomir.nikov was then you can handle this in code by inspecting the HTTP response headers e.g. watch out for **x-ratelimit-remaining** and when it nears 0 you may be able to wait until the **x-ratelimit-reset** time before retrying, we mention this technique here [https://auth0.com/docs/troubleshoot/customer-support/operational-policies/rate-limit-policy#review-http-response-headers](https://auth0.com/docs/troubleshoot/customer-support/operational-policies/rate-limit-policy#review-http-response-headers)

Warm regards.

---

<div class="post-metadata">

**Author:** ![merton](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@merton](https://community.auth0.com/u/merton)\
**Post date:** [February 16, 2024, 9:30pm UTC](https://community.auth0.com/t/what-is-the-best-way-of-retrieving-user-roles/88579/7 "2024-02-16T21:30:41Z")

</div>

In a similar situation creating an alternative admin dashboard.  
For anyone still looking for an alternative solution here, I think it’s better if we flip this around.

Instead of trying to get all users and their roles, you can get all roles, and then get all those role’s members. Combining this with getting all users allows you to do 3 requests instead on `n` user requests. This also works if you want to get all your user’s organizations. Get all orgs, then get their members.

- [Get Roles](https://auth0.com/docs/api/management/v2/roles/get-roles)
- [Get a role’s users](https://auth0.com/docs/api/management/v2/roles/get-role-user)

---

<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:** [May 15, 2024, 1:08pm UTC](https://community.auth0.com/t/what-is-the-best-way-of-retrieving-user-roles/88579/8 "2024-05-15T13:08:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dawid.matuszczyk](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dawid.matuszczyk/32/74550_2.png) [@dawid.matuszczyk](https://community.auth0.com/u/dawid.matuszczyk)\
**Post date:** [July 11, 2024, 1:12pm UTC](https://community.auth0.com/t/what-is-the-best-way-of-retrieving-user-roles/88579/9 "2024-07-11T13:12:34Z")

</div>

Hi folks!

I just wanted to update this topic, if you want to fetch the members who belong to an organization you use the [`ENDPOINT GET /api/v2/organizations/{id}/members`](https://auth0.com/docs/api/management/v2/organizations/get-members#endpoint)

This endpoint will allow you to list organization members. By setting the settings parameter `fields=roles,` you’ll retrieve the roles assigned to each listed member.

> To use this parameter, you must include the `read:organization_member_roles` scope in the token.

Thanks  
Dawid
