# How to see users role in react frontend

**URL:** <https://community.auth0.com/t/how-to-see-users-role-in-react-frontend/99909>\
**Category:** Get Help\
**Tags:** user-profile, user-management\
**Created:** [February 7, 2023, 4:23pm UTC](https://community.auth0.com/t/how-to-see-users-role-in-react-frontend/99909 "2023-02-07T16:23:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fornitori](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@fornitori](https://community.auth0.com/u/fornitori)\
**Post date:** [February 7, 2023, 4:23pm UTC](https://community.auth0.com/t/how-to-see-users-role-in-react-frontend/99909/1 "2023-02-07T16:23:49Z")

</div>

Hello, i would like to see whic role a user has, for then showing things in react only if a certain role is enabled for that user.

like this  
`if (user.role1) {

```
return (
  <React.Fragment>
    <h1>The role 1 is enabled</h1>
  </React.Fragment>
)

```

};`

i tried with the managment api to get roles but that doesn’t work and is not good to call api managment from single page applications, i tried to add roles to metadata but i don’t know how to acces those roles,

somone can help me?

---

<div class="post-metadata">

**Author:** ![fornitori](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@fornitori](https://community.auth0.com/u/fornitori)\
**Post date:** [February 8, 2023, 2:50pm UTC](https://community.auth0.com/t/how-to-see-users-role-in-react-frontend/99909/3 "2023-02-08T14:50:07Z")

</div>

SOLVED  
i added a rule in the auth0 library like this

```
exports.onExecutePostLogin = async (event, api) => {
  const namespace = 'https://myroles.com';
  if (event.authorization) {
    api.idToken.setCustomClaim(`${namespace}/roles`, event.authorization.roles);
    api.accessToken.setCustomClaim(`${namespace}/roles`, event.authorization.roles);
  }
}

```

then in react you can view it in the user from useAuth0() hook:

```
import './App.css';
import './Loader.css';
import React, { } from 'react';
import LoginButton from './Components/Login/Login';
import LogoutButton from './Components/Login/Logout';
import { useAuth0 } from "@auth0/auth0-react";
import MainHeader from './Components/MainHeader/MainHeader';
 

function App() {

  const { user, isAuthenticated, isLoading } = useAuth0();

  if (isLoading) {
    return (
      <React.Fragment>
        <br/>
        <div className="lds-roller"><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div></div>
      </React.Fragment>
    );
  } 
  else if (!isAuthenticated) {
    return (
      <React.Fragment>
        <MainHeader />
        <h1>Please login!</h1>
        <LoginButton />
      </React.Fragment>
    )
  } 
  else if (isAuthenticated) {
    // <GetRole id={user.sub}/>
    // GetRoleP(user.sub);
    const roles = user['https://myroles.com/roles'];
    let nsIsEnabled = roles.includes('nos-security');

    if (nsIsEnabled){
      return (
        <React.Fragment>
          <MainHeader />
          <h1>Welcome {user.name}</h1>
          <h1>Nos security enabled</h1>
          <LogoutButton />
        </React.Fragment>
      )
    } else {
      return (
        <React.Fragment>
          <MainHeader />
          <h1>Welcome {user.name}</h1>
          <LogoutButton />
        </React.Fragment>
      )
    };
  };
}

export default App;

```

---

<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 8, 2023, 4:54pm UTC](https://community.auth0.com/t/how-to-see-users-role-in-react-frontend/99909/4 "2023-02-08T16:54:46Z")

</div>

Hey there @fornitori welcome to the community and thanks a bunch for following up with your solution 🚀

Minor detail: For anyone looking at the extensibility bit to add roles to tokens, this is actually an Action.

> **[Auth0 Actions](https://auth0.com/docs/customize/actions)**
>
> Describes Auth0 Actions, which are secure, tenant-specific, self-contained functions that allow you to customize the behavior of Auth0.

---

<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:** [February 22, 2023, 4:55pm UTC](https://community.auth0.com/t/how-to-see-users-role-in-react-frontend/99909/5 "2023-02-22T16:55:30Z")

</div>

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