Next.js Discord

Discord Forum

how to protect public route in nextjs

Answered
bscdev#1145 posted this in #help-forum
Open in Discord
Hello Everyone, I have a nextjs school management application where client wants people with certian role should not accesss home page public route. For example student should only access their dashboard page. and I made it with authentication.
But school management also want user role=student should not also access home public page.

I know how to prevent access in authenticated pages from certain user role. But could not get an idea of protecting a public page.

How can i protect public page "home"(no authentication required) for only user role=student. Please help.
Answered by joulev
Check if request to / is authenticated with role=student, if yes then redirect
View full answer

7 Replies

Check if request to / is authenticated with role=student, if yes then redirect
Answer
@joulev ,Great idea!. Thanks for your reply. I appreciate you for the prompt reply.
@joulev Actually the page is public ...so I can not check is aurhenticated witj role=student. I would check the same if it would be a private page.
middleware runs on edge which is the reason vercel advertised nextjs as "dynamic at the speed of static"
Ok I got it. Sure I will try it. Thanks.