Auth0 Home Blog Docs

Retrieve users by role and connection

Hi,

I am trying to retrieve users by role using this endpoint /api/v2/roles/{id}/users
However, it returns all users within my tenant. The intent is to only retrieve users by role within a specific connection.

The other option is to try to use the getUsers end point, filtered by connection. But we can’t seem to access the roles when we get the info back.

Per a few other discussions, I’ve also tries t include the context.authorization.roles as a custom claim to the idToken and accessToken, but I’m not sure what I’m missing. Still haven’t found a solution to this.

Pls advice.
Thanks
Nandini

Hi @nandini,

If you simply want to check if a user has a role the easiest way is going to be adding it to the token. I would be happy to troubleshoot any issues you are running into regarding that solution. First look here:

If that isn’t the solution you need, then you will have to make multiple calls to the management api, and do some scripting to get that to work. I would be happy to work through a solution if that sounds better.

Let me know,
Dan

Thank you Dan.
Unfortunately, adding Roles to the id and access tokens are still not pulling the role with the user profile. I feel like I’m missing something. The goal is to get a list of users who have a specific role, within a specific connection. Ours is a B2B solution. Been stuck with this for a few days now…

Okay that added context changes how you are going to have to do this. Adding the roles to the access token is not going to provide a solution for this case.

I ended up looking into this pretty heavily because I thought there would be a fairly strait forward way to accomplish the goal. I ended up having to get the users from get roles' users and then doing a search for their ids and the connection. I don’t think this would scale so probably not recommended.

The best option I can think of for this specific case would be to add the role as metadata. This way you can use getUsers search to access it. Depending on how you assign roles this could be done automatically for new users via a Rule.

Let me know if you need further detail.

Thanks,
Dan