# Role-Based routing on my Next.js application

**URL:** <https://community.auth0.com/t/role-based-routing-on-my-next-js-application/101650>\
**Category:** Get Help\
**Tags:** user-authorization-rbac, directory-authorization\
**Created:** [March 2, 2023, 3:22pm UTC](https://community.auth0.com/t/role-based-routing-on-my-next-js-application/101650 "2023-03-02T15:22:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bardiya](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/bardiya/32/45068_2.png) [@bardiya](https://community.auth0.com/u/bardiya)\
**Post date:** [March 2, 2023, 3:22pm UTC](https://community.auth0.com/t/role-based-routing-on-my-next-js-application/101650/1 "2023-03-02T15:22:51Z")

</div>

Hey community.  
I’m trying to have different routes for admins and users on my Next.js app.  
I have two main routes. `/admin/*` and `/dashboard/*`. I have some Rules on my Auth0 application that adds the user role to its claims.  
Now on my application, I want to limit users. So the normal users can’t access admin routes.

I don’t want use `useUser` hook on the client-side and do the redirect if their role is not Admin. I want to do in a more cleaner way so users don’t see a glimpse of admins pages and then get redirected to other routes. I want to do it on the server-side.  
I thought maybe using middlewares could be a good option for me. But I wasn’t successfule.  
Here’s the simple code on my middleware.

```js
export default withMiddlewareAuthRequired(async function middleware(req) {
    const res = NextResponse.next();
    const { user } = await getSession(req, res) as Session;
    const userRoles = getUserRoles(user as UserProfile)
    const requestedPath = req.nextUrl.pathname

    if(requestedPath.includes("admin") && userRoles.includes(UserRoles.ADMIN)){
        return res
    } else {
        NextResponse.redirect(new URL("/dashboard", req.url))
    }
});

export const config = {
    matcher: ['/admin/:path*', '/dashboard/:path*'],
}

```

Do you have any idea why it’s not working? Or even do you have a better way to achieve it?

---

<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:** [February 18, 2025, 7:27pm UTC](https://community.auth0.com/t/role-based-routing-on-my-next-js-application/101650/3 "2025-02-18T19:27:11Z")

</div>

Hi @bardiya

Welcome to the Auth0 Community!

I am sorry about the late reply to your post!

If you still need assistance with your issue or for anybody else who may wonder, I would recommend to review the following community post:

- [Retrieve user roles and permission on profile with nextjs](https://community.auth0.com/t/retrieve-user-roles-and-permission-on-profile-with-nextjs/92070)

For more information on the matter, you can review this [knowledge article](https://auth0.com/docs/get-started/apis/enable-role-based-access-control-for-apis#dashboard) and this post from [StackOverflow](https://stackoverflow.com/questions/69033030/create-role-based-authorization-in-auth0-and-nextjs) which addressed the same issue.

If you have any other questions, feel free to reply or to post again on the community.

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:** [March 4, 2025, 7:27pm UTC](https://community.auth0.com/t/role-based-routing-on-my-next-js-application/101650/4 "2025-03-04T19:27:39Z")

</div>

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