Quick way to protect all routes under a certain parent route in nextjs (app router)?

Is there any way I can try and automatically run a function (I guess some kind of middleware) to determine a user's level of auth (I'll have access to this via an API call), and then prevent the render of said page and redirect, only under a certain route and its children?
6 Replies
Christoph
Christoph•17mo ago
You can redirect in the middleware
CamBlackwood
CamBlackwoodOP•17mo ago
thanks at @xchristoph , I had a hunch that'd be the case. Are there any examples you know of online off the top of your head? I'm looking for some now
CamBlackwood
CamBlackwoodOP•17mo ago
Routing: Middleware
Learn how to use Middleware to run code before a request is completed.
Christoph
Christoph•17mo ago
Should be documented rather well in the official docs.
CamBlackwood
CamBlackwoodOP•17mo ago
Ignore me, found the right docs 😅
Christoph
Christoph•17mo ago
It's basically just configuring the correct matcher, one if-statement and a Response.redirect call.
Want results from more Discord servers?
Add your server