# Proper way to check for roles/permissions on nextjs backend

**URL:** <https://community.auth0.com/t/proper-way-to-check-for-roles-permissions-on-nextjs-backend/96773>\
**Category:** Get Help\
**Tags:** apis, application\
**Created:** [December 20, 2022, 1:01am UTC](https://community.auth0.com/t/proper-way-to-check-for-roles-permissions-on-nextjs-backend/96773 "2022-12-20T01:01:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![faabiopk](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/faabiopk/32/40579_2.png) [@faabiopk](https://community.auth0.com/u/faabiopk)\
**Post date:** [December 20, 2022, 1:01am UTC](https://community.auth0.com/t/proper-way-to-check-for-roles-permissions-on-nextjs-backend/96773/1 "2022-12-20T01:01:59Z")

</div>

Hello,

In my application, I have a set of roles and permissions, which I added to my identity token claims using custom flows. These are used to hide/show UI elements. I am using NextJS and the auth0-nextjs SDK.

I am not sure what is the proper way of checking authorization on the backend. Of course I have the `withApiAuthRequired` wrapper for the functions, but I am not sure how should I check if a user has a specific permission. Is there a simpler way of doing that instead of doing an API call to the ManagementAPI? Can I use the accessToken somehow? It does not seem to be a JWT.

Thanks!

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [December 20, 2022, 4:57pm UTC](https://community.auth0.com/t/proper-way-to-check-for-roles-permissions-on-nextjs-backend/96773/3 "2022-12-20T16:57:05Z")

</div>

Hi @faabiopk,

Thanks for reaching out to the Auth0 Community!

I understand you need help getting the user Roles/Permissions on your Next.js app.

I recommend using the `getSession` method to get the Roles/Permissions in the custom claim from the ID Token. For example:

```auto
import { getSession } from '@auth0/nextjs-auth0';

// pages/admin.js
export default function Admin({ error }) => error ? <div>error<div> : <div>Admin Section</div>;

export getServerSideProps = ({ req, res }) => {
  const session = getSession(req, res);
  if (!session?.user['http://your-namespace/roles'].includes('admin')) {
    return { props: { error: 'Forbidden' } }
  }
}

```

I hope this helps!

Please let me know how this goes for you.

Thanks,  
Rueben

---

<div class="post-metadata">

**Author:** ![faabiopk](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/faabiopk/32/40579_2.png) [@faabiopk](https://community.auth0.com/u/faabiopk)\
**Post date:** [January 4, 2023, 11:42pm UTC](https://community.auth0.com/t/proper-way-to-check-for-roles-permissions-on-nextjs-backend/96773/5 "2023-01-04T23:42:44Z")

</div>

Hi Rueben!

Yes, I need help, but in the backend instead. I was able to use the roles/permissions on the frontend without problems.

I am not sure how to use them on the backend of NextJS, could you point me to the direction? This information is on the ID token, right? On the backend I should NOT use it for authorization, correct?

Thank you

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [January 5, 2023, 6:11pm UTC](https://community.auth0.com/t/proper-way-to-check-for-roles-permissions-on-nextjs-backend/96773/6 "2023-01-05T18:11:57Z")

</div>

Hi @faabiopk,

Thank you for your response.

For the backend, I recommend using the Management API’s [Get a user’s roles](https://auth0.com/docs/api/management/v2#!/Users/get_user_roles) and [Get a User’s Permissions](https://auth0.com/docs/api/management/v2#!/Users/get_permissions) endpoints.

Reference docs:

- [Get Management API Access Tokens for Production](https://auth0.com/docs/secure/tokens/access-tokens/get-management-api-access-tokens-for-production) doc for more details.
- [View User Permissions](https://auth0.com/docs/manage-users/access-control/configure-core-rbac/rbac-users/view-user-permissions)
- [View Roles Assigned to Users](https://auth0.com/docs/manage-users/access-control/configure-core-rbac/rbac-users/view-user-roles)

And to reiterate, you could append the user’s Roles and Permissions as custom claims in the Access Token. Please make sure to include the `audience` parameter when requesting an access token for it to be a JWT. See [this doc](https://auth0.com/docs/secure/tokens/access-tokens/get-access-tokens#control-access-token-audience) for details.

Thanks,  
Rueben

---

<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:** [January 19, 2023, 6:12pm UTC](https://community.auth0.com/t/proper-way-to-check-for-roles-permissions-on-nextjs-backend/96773/7 "2023-01-19T18:12:25Z")

</div>

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