Header
{
  host: 'localhost:8080',
  connection: 'keep-alive',
  'user-agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 18_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.5 Mobile/15E148 Safari/604.1',
  accept: 'application/json',
  origin: 'http://localhost:5173',
  'sec-fetch-site': 'same-site',
  'sec-fetch-mode': 'cors',
  'sec-fetch-dest': 'empty',
  referer: 'http://localhost:5173/',
  'accept-encoding': 'gzip, deflate, br, zstd',
'accept-language': 'en-US,en;q=0.9' } session: { cookieCache: { enabled: true, maxAge: 20 * 60 * 60, // Cache duration in seconds }, }, // The connection between the frontend and the backend. app.use(cors({ origin: process.env.FRONTEND_URL, credentials: true })); const session = await auth?.api?.getSession({ headers: fromNodeHeaders(req.headers), }); console.log("sess", session); These are every thing am getting session null in the server side express. js @Better Auth
'accept-language': 'en-US,en;q=0.9' } session: { cookieCache: { enabled: true, maxAge: 20 * 60 * 60, // Cache duration in seconds }, }, // The connection between the frontend and the backend. app.use(cors({ origin: process.env.FRONTEND_URL, credentials: true })); const session = await auth?.api?.getSession({ headers: fromNodeHeaders(req.headers), }); console.log("sess", session); These are every thing am getting session null in the server side express. js @Better Auth
2 Replies
Hi there! It looks like your Express backend isn’t recognizing your session because the session token isn’t being correctly passed through the request headers. Here are a few points to double-check:
1. Make sure that your client is sending the session cookie in its request to the backend. In the header sample you provided, there isn’t any "Cookie" field. If the cookie (typically named something like “better‑auth.session_token”) isn’t present, Better Auth won’t be able to fetch the session [7].
2. Confirm that your CORS configuration exactly matches the request’s origin (in your case, "http://localhost:5173") and that credentials are enabled. Misaligned CORS settings can sometimes lead to cookies not being forwarded properly [3], [5].
3. Since you mentioned you’ve set up the session configuration with cookie cache enabled, that’s a good step. As noted in one of our reports, if “session.cookieCache.enabled” isn’t set to true, getSession will return null [4]. It appears you already have this configured correctly, so the focus should be on the headers.
4. Finally, verify that your client-side fetch (or whatever HTTP client you’re using) is set to include cookies. For instance, if you’re using fetch, make sure to set credentials to "include".
Could you check whether the session cookie is present in your actual request headers and if the client is configured to forward credentials? Let me know if you need more help—have you been able to log the incoming headers on your Express server to confirm the presence of the cookie? If issues persist, feel free to tag @bekacru or @ping__ for further assistance!
GitHub
better-auth/better-auth
The most comprehensive authentication framework for TypeScript - better-auth/better-auth
GitHub
better-auth/better-auth
The most comprehensive authentication framework for TypeScript - better-auth/better-auth
GitHub
better-auth/better-auth
The most comprehensive authentication framework for TypeScript - better-auth/better-auth
GitHub
better-auth/better-auth
The most comprehensive authentication framework for TypeScript - better-auth/better-auth
Sources:
[3] auth.api.getSession() returns null despite valid session token in database and successful HTTP endpoint response
[4] auth.api.getSession always returns null if session.cookieCache is not set in the 
auth
[5] auth.api.getSession() returns null despite valid session token in database and proper cookie headers
[7] Better Auth cookies caching from Expo app (client) to Next JS API (server)
— Better Auth is powered by https://clarm.comGitHub
auth.api.getSession() returns null despite valid session token in d...
Is this suited for github? Yes, this is suited for github To Reproduce: Set up BetterAuth with Prisma adapter and MySQL database Configure Express server with BetterAuth routes at /api/auth/* Creat...
GitHub
auth.api.getSession always returns null if session.cookieCache is n...
Is this suited for github? Yes, this is suited for github To Reproduce Log in using authClient.signIn.social successfully Navigate into dashboard route Calls await auth.api.getSession results null ...
GitHub
auth.api.getSession() returns null despite valid session token in d...
Is this suited for github? Yes, this is suited for github To Reproduce Create a user account: curl -X POST http://localhost:3000/api/v1/auth/signup \ -H "Content-Type: application/json" ...
GitHub
Better Auth cookies caching from Expo app (client) to Next JS API (...
Is this suited for github? Yes, this is suited for github To Reproduce To reproduce Step 1: Setup Better Auth with Expo + Next.js // lib/auth/auth.js import { betterAuth } from "better-auth&qu...