# Auth0 Action add user role to token

**URL:** <https://community.auth0.com/t/auth0-action-add-user-role-to-token/130145>\
**Category:** Get Help\
**Tags:** user-profile, user-management\
**Created:** [March 21, 2024, 7:52am UTC](https://community.auth0.com/t/auth0-action-add-user-role-to-token/130145 "2024-03-21T07:52:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidplane](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/davidplane/32/61188_2.png) [@davidplane](https://community.auth0.com/u/davidplane)\
**Post date:** [March 21, 2024, 7:52am UTC](https://community.auth0.com/t/auth0-action-add-user-role-to-token/130145/1 "2024-03-21T07:52:04Z")

</div>

[[Sample Use Cases: Actions with Authorization](https://auth0.com/docs/manage-users/access-control/sample-use-cases-actions-with-authorization#add-user-roles-to-tokens)]

I am using nextjs with next-auth, and followed this guide above to add user role to token. Also assigned my account to an admin role under User Management/Roles on the auth0 dashboard

```auto
import useAuth from "../context/useAuth";

export default function Profile() {
  const { session, status, update } = useAuth();

  const role = !session?.user["https://<website>/roles"];

  if (session) {
    return (
      <>
        <div>Hello {session.user.name}</div>
        <div>Role: {role}</div>
      </>
    );
  }
}

```

useAuth.js

```auto
import { useSession, signIn, signOut } from "next-auth/react";

const useAuth = () => {
  const { data: session, status, update } = useSession();

  return { session, status, update, signIn, signOut };
};

export default useAuth;

```

I’m able to get the name from session.user.name, but can’t seem to retrieve the role as https:///roles returns an error and the page can’t even load. Is this the correct way to retrieve roles?

Running the test in auth0 actions with a random user returns this which seems to be working

```auto
[
  {
    "name": "https://<website>/roles",
    "target": "idToken",
    "type": "SetCustomClaim",
    "value": []
  },
  {
    "name": "https://<website>/roles",
    "target": "accessToken",
    "type": "SetCustomClaim",
    "value": []
  }
]

```

---

<div class="post-metadata">

**Author:** ![marcelina.barycka](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/marcelina.barycka/32/56162_2.png) [@marcelina.barycka](https://community.auth0.com/u/marcelina.barycka)\
**Post date:** [March 25, 2024, 12:39pm UTC](https://community.auth0.com/t/auth0-action-add-user-role-to-token/130145/4 "2024-03-25T12:39:26Z")

</div>

Hey there @davidplane , thanks for posting!

Let’s discover what’s missing.

> [@davidplane](#):
>
> Also assigned my account to an admin role under User Management/Roles on the auth0 dashboard

Alright, so the user you are testing the authorization has some role assigned👍🏼

We would also want to discover what properties are available under the session object (if the added claim can be reachable there) and if this can be printed out to the user this way.

Before we dig into that - could you please:

- make sure the Action is deployed and [added](https://auth0.com/docs/customize/actions/write-your-first-action#attach-the-action-to-a-flow) to the Flow?
- the API that the user tries to authorize to have the RBAC feature enabled? This can be done via Auth0 dashboard → Applications → API → your API → Settings → toggle the switch of RBAC.

Could also try to verify if the claim is added to the token by decoding it via [jwt.io](https://jwt.io/) - tokens should be available via the browser developer tool under the https://auth0\_domain/oauth/token (response).

 ![image](https://global.discourse-cdn.com/auth0/original/3X/b/8/b87bd55e40a5e9ac1f2982428cee0f825ead3560.png)

Looking forward to your reply!

---

<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:** [April 8, 2024, 12:40pm UTC](https://community.auth0.com/t/auth0-action-add-user-role-to-token/130145/5 "2024-04-08T12:40:08Z")

</div>

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