useSession returns null
Anyone knows why useSession returns null?
1) no cookie prefix or any cookie name customization
2) i can see cookie logs(in middleware)
3)but i get null from useSession
4) auth client looks like this

Solution:Jump to solution
thanks! i realize that when you render something depdening on the session data and it's null at the begining, it just stops there and wont continue
21 Replies
auth looks like this:
and it's latest version 1.2.5
Where are you using useSession at?
some tsx component with "use client";
Try moving
nextCookies
to the end of your plugins array.still not working.
the component looks like
Can you check what the error says if it exists? It's another option you can get out of useSession.
wait. i thought you are saying "remove". let me try one more time
unfortunately no. i can see cookie is there from middleware loggings after signing in.
GET /auth/login 200 in 971ms
GET /api/auth/get-session 200 in 13ms
🍪 Session cookie: null
🍪 Session cookie: null
session from auth server in LoginPage null
GET /auth/login 200 in 75ms
{"level":30,"time":1743794920050,"pid":64910,"hostname":"Weizhis-Mac-mini.local","msg":"Sending verification OTP to:"}
POST /api/auth/email-otp/send-verification-otp 200 in 34ms
{"level":30,"time":1743794920050,"pid":64910,"hostname":"Weizhis-Mac-mini.local","msg":"139991"}
{"level":30,"time":1743794920050,"pid":64910,"hostname":"Weizhis-Mac-mini.local","msg":"sign-in"}
POST /api/auth/sign-in/email-otp 200 in 39ms
🍪 Session cookie: Qu5xzZad3h3dVScTSuGRjvwxM8rlXH68.6ngMzfl7O9MP6zUNTlGGiYsUa8v8U%2FAn%2BOKibCqJYPI%3D
GET /dashboard 200 in 31ms
GET /api/auth/get-session 200 in 14ms
GET /api/auth/get-session 200 in 17ms
but not from useSession
i dont see any error tho
Can you set the entire result of useSession as 1 variable and log that variable? I want to check something.
it's already one variable.
const { data: authClientSession } = authClient.useSession();
console.log(
"session from auth client in NavUser",
JSON.stringify(authClientSession, null, 2),
and it's null
Don't destructure it. I mean to make the whole result of useSession 1 variable.
ah
const data = authClient.useSession();
console.log(
"data from auth client in NavUser",
JSON.stringify(data, null, 2),
);
Do you get any logs after that? Anytime it's
pending
the data will just be null
no
Can you check network panel in your devtools to see if the get-session requests resolves?
And maybe also check if there is a response in that request.
Solution
thanks! i realize that when you render something depdening on the session data and it's null at the begining, it just stops there and wont continue
it's all working now. thank you @Ping
How did you manage to egt it working?
Hello! I have a similar issue. The OTP is sent and the sign in works. The user and a session is created in the database but no cookies and the useSession is null.
I'm using a server action to sign-in that looks like :
My auth.ts looks like :
And the authClient as :
You're using authClient in a server action.
Any server related actions should use
auth.api
insteadThanks @Ping ! I'm working on it and will be back if it works
@Ping 😆 Awesome. It works. You made my day. This is my implementation. Any refactoring?
Looks good to me 🫡