useSession weird behaviour
Hi, I have some problems with useSession, it keeps giving me date null even though I am logged in. I'm testing it with dev and turbopack.
This happens when I do a page refresh, so if I logout and log in the hook works, but if I do a page refresh it no longer works returning null.
auth-client.ts
auth.ts

10 Replies
Hey, you are trying to access the
useSession
in a server context, which is a hook reserved to client side.
What you should do, in a server context, is something like this :
If you really want to use useSession()
, you should add "use client"
in the top of your fileNope, it's a client component
That's why i specified client component

Here you go đŸ™‚
Oh okay, so in the first place you cannot do
session.data!.user.id
because in the first render, session.data
will not be defined, since it's client side it will have a little throttle (bcs of the cookie storage).
So the error say that he's waiting for the user is right.
Here's what i did :
Ok but the user will be null anyway
export function Notifications() {
const user = useCurrentUser()
const { data: notifications, refetch } = useNotificationsQuery(user.id)
I already have that hook but the user returns null
so i can't conditionally call hooks
Does it work if you call
useCurrentUser
right in useNotificationsQuery
?No because user is null
The argument of type ‘string | undefined’ is not assignable to the parameter of type ‘string’.
The type ‘undefined’ is not assignable to the type ‘string’.
Is it possible for you to provide a minimal reproduction example via github ? I'll be happy to help
If you're using TanStack Query I suggest my package for useSession https://github.com/daveyplate/better-auth-tanstack