Easy way to prevent access to multiple pages at once in Next App router
Answered
Smalandstövare posted this in #help-forum
SmalandstövareOP
Hello everyone, as the title suggest, I'm looking for a way to prevent users access to many pages at once. Ideally I'd like to redirect them to certain page in case they try to access some forbidden page.
One solution would be to call router.redirect in every page, but that looks like a bad practice since this redirect will stay only for ~2 weeks.
Does Next have something like a page middleware where by using a pattern I could make a redirect rule?
For example, I'd like to prevent access to all pages except home page ( / )
One solution would be to call router.redirect in every page, but that looks like a bad practice since this redirect will stay only for ~2 weeks.
Does Next have something like a page middleware where by using a pattern I could make a redirect rule?
For example, I'd like to prevent access to all pages except home page ( / )
Answered by not-milo.tsx
Since you're trying to protect routes and allow access only to authenticated user, using the middleware is probably the best option. You can check if a visitor has a valid auth session and redirect them if they don't have it.
I'm afraid plain rewrites won't be enough...
I'm afraid plain rewrites won't be enough...
4 Replies
Since you're trying to protect routes and allow access only to authenticated user, using the middleware is probably the best option. You can check if a visitor has a valid auth session and redirect them if they don't have it.
I'm afraid plain rewrites won't be enough...
I'm afraid plain rewrites won't be enough...
Answer
SmalandstövareOP
You're right. That's probably the easiest solution 🙂