Next.js Discord

Discord Forum

Protect pages from a logged in user

Answered
Bigheaded ant posted this in #help-forum
Open in Discord
Bigheaded antOP
Hi, I can't my wrap my head around how I can redirect users from login or register page to home if they are logged in already. I want to do this via a middleware. I tried to use getServerSession but that does not work in middleware.

I also used next-auth middleware but i am only able to protect pages from unauthenticated users not the opposite.
Answered by joulev
use getToken it should work for middleware
View full answer

7 Replies

Answer
because last time i checked, the next-auth default middleware uses getToken behind the scenes, so that function is definitely edge-compatible
Bigheaded antOP
I tried getToken before but it kept giving me null now i tried again on your suggestion and it works.

I don't know if this next framework has inconsistencies or I am going insane. I feel so mentally exhausted 😫 .
:this_is_fine: Thank you anyways 🙏
well... whatever happened in the past we don't need to care, as long as it works we are good
have fun coding
Bigheaded antOP
Thank you.