# Accessing users role post-login

**URL:** <https://community.auth0.com/t/accessing-users-role-post-login/185720>\
**Category:** Get Help\
**Tags:** classic-universal-login-experience, login-experience\
**Created:** [April 20, 2025, 8:00pm UTC](https://community.auth0.com/t/accessing-users-role-post-login/185720 "2025-04-20T20:00:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![RagnarSmari](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ragnarsmari/32/74384_2.png) [@RagnarSmari](https://community.auth0.com/u/RagnarSmari)\
**Post date:** [April 20, 2025, 8:00pm UTC](https://community.auth0.com/t/accessing-users-role-post-login/185720/1 "2025-04-20T20:00:32Z")

</div>

I have been trying to implement RBAC to my application.  
I have an API and a Frontend, the APi has access to the management API but the frontend is a typical NextJs application.

I am trying to get the currently logged in users role in the middleware through the getSession() hook.

I have added the roles to the accesstoken and the idtoken, when I log in and console log those values the roles have indeed been added to the tokens correctly. So I have set the action correctly and everything.  
What I don’t get is why can’t I access it in the user object? Am I supposed to parse the jwt token myself?  
I’m not sure what is left to check.

1. Created the roles and added them to my user.
2. Created a action and assign it to post-login trigger via auth0 dashboard.
3. Initialize the auth0 sdk within nextjs:

```auto
import { Auth0Client } from "@auth0/nextjs-auth0/server"

export const auth0 = new Auth0Client({
    authorizationParameters: {
        audience: process.env.AUTH0_AUDIENCE,
        scope: 'openid profile email roles'
    },
})

```

- note that the audience comes from the same api which I’m using for my server.

1. Create the middleware and fetch the roles for the user.

```auto
import {NextRequest, NextResponse} from "next/server"

import { auth0 } from "./lib/auth0"

export async function middleware(request: NextRequest) {
    const authRes = await auth0.middleware(request)
    if (request.nextUrl.pathname.startsWith("/auth")) {
        return authRes
    }
    
    const session = await auth0.getSession()
    if (!session){
        return NextResponse.redirect(new URL("/auth/login", request.nextUrl.origin))
    } else {
        const roles: string[] = session?.user['https://your-app-name.com/roles'];
        console.log(roles)
        console.log(session.user)
        console.log(session.tokenSet.accessToken)
    } 
}

```

Am I missing something crucial in the flow?

---

<div class="post-metadata">

**Author:** ![RagnarSmari](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ragnarsmari/32/74384_2.png) [@RagnarSmari](https://community.auth0.com/u/RagnarSmari)\
**Post date:** [April 20, 2025, 8:03pm UTC](https://community.auth0.com/t/accessing-users-role-post-login/185720/2 "2025-04-20T20:03:16Z")

</div>

> [@RagnarSmari](#):
>
> ```auto
> console.log(roles)
> console.log(session.user)
> console.log(session.tokenSet.accessToken)
> 
> ```

```auto
        console.log(roles)
        console.log(session.user)
        console.log(session.tokenSet.accessToken)

```

Forgot to add:

1. The roles const comes off as undefined.
2. The user object does not have my namespace.
3. When I parse the accesstoken I get the namespace with the roles for the user.

---

<div class="post-metadata">

**Author:** ![nik.baleca](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/nik.baleca/32/68749_2.png) [@nik.baleca](https://community.auth0.com/u/nik.baleca)\
**Post date:** [April 21, 2025, 8:54pm UTC](https://community.auth0.com/t/accessing-users-role-post-login/185720/4 "2025-04-21T20:54:42Z")

</div>

Hi @RagnarSmari

Welcome to the Auth0 community!

How are you setting the roles in an action? You would assign the roles as such:

```auto
exports.onExecutePostLogin = async (event, api) => {

 api.idToken.setCustomClaim("https://randomtest.org/roles", event.authorization.roles)

}

```

By any chance, do the naming convention for the namespace when setting the custom claims using a PostLogin Action go against our restrictions mentioned in this [documentation](https://auth0.com/docs/secure/tokens/json-web-tokens/create-custom-claims#namespaced-guidelines)?

Otherwise, could you try assigning the roles like this:

```auto
  const session = getSession(req, res);
  
  console.log(session?.user['{{YOUR_NAMESPACE}}']

```

Also, session.user in this case should contain the custom claims you have set using the PostLogin Action.

Alternatively, you can try to retrieve this data using the `useUser()` hook mentioned in the [Github Documentation](https://auth0.github.io/nextjs-auth0/functions/client.useUser.html).

Let me know if you have any other questions or if the solutions above were not helpful!

Kind Regards,  
Nik

---

<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:** [May 5, 2025, 8:55pm UTC](https://community.auth0.com/t/accessing-users-role-post-login/185720/5 "2025-05-05T20:55:02Z")

</div>

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