Next.js Discord

Discord Forum

useSession() from NextAuth fails

Answered
Gull Dong posted this in #help-forum
Open in Discord
Avatar
Gull DongOP
I am trying to return the session: Session object from within my function, however, I keep getting the rule of hooks error.
Image
Image
Answered by Alfonsus Ardani
use getServerSession if you want to get it from the server
View full answer

32 Replies

Avatar
where are you using Token function
Avatar
Gull DongOP
in a file
Avatar
:doh:
Avatar
Gull DongOP
lol im trying to export it as a module
i could also just import a function that runs the checks im trying to run but i wanted to test this^
Avatar
rename Token to useToken
Avatar
Gull DongOP
did that and got same error
Avatar
ok let me see where you are using your Token function
Avatar
Gull DongOP
Image
Avatar
you cant do that
Avatar
Gull DongOP
why nawt
Avatar
because rule of hooks
Avatar
Gull DongOP
i dont get which rule im breaking tho
Avatar
you can't use hooks outside of a react component
Avatar
Gull DongOP
nextauth also doesnt let you return user's session in server call so oh well
Avatar
use getServerSession if you want to get it from the server
Answer
Avatar
but oh well
Avatar
Gull DongOP
getserversession only returns boolean no?
Avatar
idk where you get that from
Avatar
Gull DongOP
the website o_o
oh but you need a request
Avatar
you can just pass in the authOption on the first parameter, no need request or response, those are for pages dir
Avatar
Gull DongOP
oh okay
Avatar
Gull DongOP
does calling getServerSession() return the same object as useSession()?
Avatar
it has minor differences but yes it should return the same crucial data
Avatar
Gull DongOP
well now im just getting a freaking loader issue
even tho i changed none of my packages
Avatar
But using it inside a react component works right?
Avatar
Gull DongOP
yeah i got the session
Avatar
also that using getServerSession() in Server Component works right?
Avatar
Gull DongOP
can you resolve the post
Avatar
allright thanks