Auth0 Home Blog Docs

Is querying user roles via /api/v2/users possible?

Hi All,

In some code I’m working on, I would like to have a list of all users matching a particular search query, and I’d like each user object to be annotated with the roles of which they are members.

I’m currently doing this by listing all the users in the roles that I care about, and then as I read the user list, I check manually if their user ID is present in each role. This works fine, however, it is inefficient for lots of users. Say I have a role with a few thousand users in it, but my user query only returns a few - I still have to query the thousands of users for the role. The alternative is that I make an API call to get each user’s role list, but if I have a lot of users, I will run out of API rate limit very quickly.

Is there a way to do this efficiently? Rules can edit tokens easily enough, but is it possible to craft a rule which appends roles to the user object?

My next attempt will be to annotate the user’s roles manually via app_metadata - which should be efficient, but I have to keep things up to date in there myself.

Thanks for any tips.

Nevermind, I think I figured it out. I added a rule to propagate roles into the app_metadata and don’t have to do the role membership queries anymore. The downside is that it only gets applied at the next login, but that’s fine.

Hi @marcinromaszewicz,

Welcome to the Auth0 Community!

Thanks for answering your own question! That is definitely a sufficient strategy to get roles in an accessible location in the user profile.

Thanks,
Dan

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