how to protect public route in nextjs
Answered
bscdev#1145 posted this in #help-forum
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.
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
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.
@bscdev#1145 <@484037068239142956> 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.
check with middleware, it will be fast as if the page was public
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.