useSession() from NextAuth fails
Answered
Gull Dong posted this in #help-forum
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
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](https://cdn.discordapp.com/attachments/1156027007012970556/1156027007163957258/part1.png?ex=6513798c&is=6512280c&hm=9424493c2138d0393305b0b7395e6a130ba77e5ebc9b283aa28f4c00e2ff76b0&)
![Image](https://cdn.discordapp.com/attachments/1156027007012970556/1156027007352705024/Screenshot_2023-09-25_193747.png?ex=6513798c&is=6512280c&hm=5b0d6ce72121db98f7c16bc624d8812187df6077db81b90562fea12494631b43&)
Answered by Alfonsus Ardani
use getServerSession if you want to get it from the server
32 Replies
![Avatar](https://cdn.discordapp.com/avatars/194128415954173952/708a8637ada505b6b7a85972807d9dca.webp?size=256)
Alfonsus Ardani
where are you using Token function
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
Gull DongOP
in a file
![Avatar](https://cdn.discordapp.com/avatars/194128415954173952/708a8637ada505b6b7a85972807d9dca.webp?size=256)
Alfonsus Ardani
![:doh:](https://cdn.discordapp.com/emojis/767751499727896596.png)
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
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](https://cdn.discordapp.com/avatars/194128415954173952/708a8637ada505b6b7a85972807d9dca.webp?size=256)
Alfonsus Ardani
rename Token to useToken
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
Gull DongOP
did that and got same error
![Avatar](https://cdn.discordapp.com/avatars/194128415954173952/708a8637ada505b6b7a85972807d9dca.webp?size=256)
Alfonsus Ardani
ok let me see where you are using your Token function
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
Gull DongOP
![Image](https://cdn.discordapp.com/attachments/1156027007012970556/1156027598225277008/image.png?ex=65137a19&is=65122899&hm=e0660b4afb31bf798e6cb8c01066de2d6c15d6af8b948402d407e77907fe0cd2&)
![Avatar](https://cdn.discordapp.com/avatars/194128415954173952/708a8637ada505b6b7a85972807d9dca.webp?size=256)
Alfonsus Ardani
you cant do that
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
Gull DongOP
why nawt
![Avatar](https://cdn.discordapp.com/avatars/194128415954173952/708a8637ada505b6b7a85972807d9dca.webp?size=256)
Alfonsus Ardani
because rule of hooks
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
Gull DongOP
i dont get which rule im breaking tho
![Avatar](https://cdn.discordapp.com/avatars/194128415954173952/708a8637ada505b6b7a85972807d9dca.webp?size=256)
Alfonsus Ardani
you can't use hooks outside of a react component
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
Gull DongOP
nextauth also doesnt let you return user's session in server call so oh well
![Avatar](https://cdn.discordapp.com/avatars/194128415954173952/708a8637ada505b6b7a85972807d9dca.webp?size=256)
Alfonsus Ardani
use getServerSession if you want to get it from the server
Answer
![Avatar](https://cdn.discordapp.com/avatars/194128415954173952/708a8637ada505b6b7a85972807d9dca.webp?size=256)
Alfonsus Ardani
but oh well
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
Gull DongOP
getserversession only returns boolean no?
![Avatar](https://cdn.discordapp.com/avatars/194128415954173952/708a8637ada505b6b7a85972807d9dca.webp?size=256)
Alfonsus Ardani
idk where you get that from
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
Gull DongOP
the website o_o
oh but you need a request
![Avatar](https://cdn.discordapp.com/avatars/194128415954173952/708a8637ada505b6b7a85972807d9dca.webp?size=256)
Alfonsus Ardani
you can just pass in the authOption on the first parameter, no need request or response, those are for
pages
dir![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
Gull DongOP
oh okay
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
Gull DongOP
does calling
getServerSession()
return the same object as useSession()
?![Avatar](https://cdn.discordapp.com/avatars/194128415954173952/708a8637ada505b6b7a85972807d9dca.webp?size=256)
Alfonsus Ardani
it has minor differences but yes it should return the same crucial data
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
Gull DongOP
well now im just getting a freaking loader issue
even tho i changed none of my packages
![Avatar](https://cdn.discordapp.com/avatars/194128415954173952/708a8637ada505b6b7a85972807d9dca.webp?size=256)
Alfonsus Ardani
But using it inside a react component works right?
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
Gull DongOP
yeah i got the session
![Avatar](https://cdn.discordapp.com/avatars/194128415954173952/708a8637ada505b6b7a85972807d9dca.webp?size=256)
Alfonsus Ardani
also that using getServerSession() in Server Component works right?
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
Gull DongOP
can you resolve the post
![Avatar](https://cdn.discordapp.com/avatars/194128415954173952/708a8637ada505b6b7a85972807d9dca.webp?size=256)
Alfonsus Ardani
allright thanks