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
Alfonsus Ardani
where are you using Token function
Avatar
Gull DongOP
in a file
Avatar
Alfonsus Ardani
: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
Alfonsus Ardani
rename Token to useToken
Avatar
Gull DongOP
did that and got same error
Avatar
Alfonsus Ardani
ok let me see where you are using your Token function
Avatar
Gull DongOP
Image
Avatar
Alfonsus Ardani
you cant do that
Avatar
Gull DongOP
why nawt
Avatar
Alfonsus Ardani
because rule of hooks
Avatar
Gull DongOP
i dont get which rule im breaking tho
Avatar
Alfonsus Ardani
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
Alfonsus Ardani
use getServerSession if you want to get it from the server
Answer
Avatar
Alfonsus Ardani
but oh well
Avatar
Gull DongOP
getserversession only returns boolean no?
Avatar
Alfonsus Ardani
idk where you get that from
Avatar
Gull DongOP
the website o_o
oh but you need a request
Avatar
Alfonsus Ardani
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
Alfonsus Ardani
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
Alfonsus Ardani
But using it inside a react component works right?
Avatar
Gull DongOP
yeah i got the session
Avatar
Alfonsus Ardani
also that using getServerSession() in Server Component works right?
Avatar
Gull DongOP
can you resolve the post
Avatar
Alfonsus Ardani
allright thanks