Next.js Discord

Discord Forum

Middleware redirects not working in NextJS app based routing

Unanswered
Rohu posted this in #help-forum
Open in Discord
RohuOP
I am experiencing an issue with the middleware. I need to route the user based on their role, but the middleware is not able to route properly. Here's the explanation:

I have two roles in my app, manager and staff, and a user can have either of these roles in their respective org.

When the user changes the org using a dropdown in the UI, the middleware is supposed to detect the new selected org and route the user to the corresponding URL. I've written the following logic for this:

if (orgRole === 'manager' && request.nextUrl.pathname.includes('/staff')) {
const url = request.nextUrl.clone()
url.pathname = ${orgShortId}/dashboard/manager/app
return NextResponse.redirect(url, { status: 303 })
}

if (orgRole === 'staff' && request.nextUrl.pathname.includes('/manager')) {
const url = request.nextUrl.clone()
url.pathname = ${orgShortId}/dashboard/staff/app
return NextResponse.redirect(url, { status: 303 })
}

However, the issue I'm facing is that the app is not getting routed to the URL as expected; it remains unchanged.


I am using app route.

2 Replies

Cicada killer
Hi @Rohu I am also facing issue like its gettig redirected to many times. Does you found any solution.
RohuOP
Not yet, I am still looking into this