# How can I get user roles client side?

**URL:** <https://community.auth0.com/t/how-can-i-get-user-roles-client-side/127863>\
**Category:** Get Help\
**Tags:** management-api, roles\
**Created:** [February 20, 2024, 3:52pm UTC](https://community.auth0.com/t/how-can-i-get-user-roles-client-side/127863 "2024-02-20T15:52:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![luke2](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@luke2](https://community.auth0.com/u/luke2)\
**Post date:** [February 20, 2024, 3:52pm UTC](https://community.auth0.com/t/how-can-i-get-user-roles-client-side/127863/1 "2024-02-20T15:52:18Z")

</div>

Hi, I’m new to using Auth0 sos still trying to get an understanding of how things work.

What i’m wanting to do is conditional checks client-side which will show/hide certain content depending on authenticated user roles.

All users will either have a role of default or admin set. If a users role is admin, then the conditional will pass using something like the below.

Is there away to get role data set on a user through `useUser`? if not, is there a different or better way to achieve what i’m trying to do?

This is for a Nextjs project.

```auto
    const { isLoading, error, user } = useUser();

    if (isLoading) return <div>Loading...</div>;
    if (error) return <div>{error.message}</div>;

if (user.roles.includes('admin')) {
  console.log('User is an admin.');
  // Perform admin-specific actions here
} 

```

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [February 24, 2024, 1:07am UTC](https://community.auth0.com/t/how-can-i-get-user-roles-client-side/127863/3 "2024-02-24T01:07:54Z")

</div>

Hey @luke2 welcome to the community!

> [@luke2](#):
>
> Is there away to get role data set on a user through `useUser`? if not, is there a different or better way to achieve what i’m trying to do?

Have you tried adding roles to tokens? In this case you’d want to focus on the ID token in particular:

[https://community.auth0.com/t/how-to-add-roles-and-permissions-to-the-id-token-using-actions/84506](https://community.auth0.com/t/how-to-add-roles-and-permissions-to-the-id-token-using-actions/84506)

`roles` should then be available via the `useUser` hook.

---

<div class="post-metadata">

**Author:** ![luke2](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@luke2](https://community.auth0.com/u/luke2)\
**Post date:** [February 26, 2024, 8:18pm UTC](https://community.auth0.com/t/how-can-i-get-user-roles-client-side/127863/4 "2024-02-26T20:18:33Z")

</div>

Yeah I managed to get it working in the end with

```auto
exports.onExecutePostLogin = async (event, api) => {
  const namespace = 'roleType';
  
  if (event.authorization) {
    api.idToken.setCustomClaim(`${namespace}`, event.authorization.roles);
    api.accessToken.setCustomClaim(`${namespace}`, event.authorization.roles);
  }
}

```

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [February 26, 2024, 8:19pm UTC](https://community.auth0.com/t/how-can-i-get-user-roles-client-side/127863/5 "2024-02-26T20:19:13Z")

</div>

That’s great, thanks for confirming!

---

<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:** [March 11, 2024, 8:20pm UTC](https://community.auth0.com/t/how-can-i-get-user-roles-client-side/127863/6 "2024-03-11T20:20:06Z")

</div>

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