Next.js Discord

Discord Forum

Check for route groups in NextRequest?

Answered
Atlantic menhaden posted this in #help-forum
Open in Discord
Atlantic menhadenOP
I want to protect my /(app) route in NextJs (I am using NextAuth). From what I found online, the way you do this is by using getServerSession() inside a middleware.(js|ts) file. I can see the req.nextUrl.hostname but I can't see the actual path it came from. I could do this on sveltekit but apparently nextjs doesn't have this?


I am using the app router
Answered by Ray
export const config = {
  matcher: ['/about/:path*', '/dashboard/:path*'],
}
View full answer

14 Replies

Atlantic menhadenOP
Where can I find the literal route so I can detect /(app) like /(app)/profile and /(app)/feed
you should use the matcher in middleware
and getServerSession will not work in middleware since its using node api which is not available in middleware
@Ray and `getServerSession` will not work in middleware since its using node api which is not available in middleware
Atlantic menhadenOP
Yea I was thinking of using this next-auth provided middleware instead
But I really enjoy those router groups
you can still use route group
Atlantic menhadenOP
How
well I mean you can still use route group for routing but use matcher in middleware for protected route
@Ray well I mean you can still use route group for routing but use matcher in middleware for protected route
Atlantic menhadenOP
I mean can I match to a group route via the matcher?
yes but not route group
export const config = {
  matcher: ['/about/:path*', '/dashboard/:path*'],
}
Answer
Atlantic menhadenOP
makes sense