Better AuthBA
Better Authโ€ข2mo ago
Vishnu

๐Ÿ†˜ Need help with Better Auth + Express + MongoDB (Session always null)

Hey everyone ๐Ÿ‘‹

I'm using Better Auth with Express, TypeScript, and MongoDB.
I've set up authentication as per the docs โ€” login works fine and I can see the session being created in my database.

However, when I call auth.api.getSession, it always returns null.
auth.ts
import { betterAuth } from "better-auth"; import { MongoClient } from "mongodb"; import { databaseConfig } from "../config/db.config.js"; import { mongodbAdapter } from "better-auth/adapters/mongodb"; const mongoClient = new MongoClient(databaseConfig.uri); const db = mongoClient.db(); export const auth = betterAuth({ user: { additionalFields: { isActive: { type: "boolean", defaultValue: false }, firstName: { type: "string", required: true }, lastName: { type: "string", required: true }, mobileNumber: { type: "string", required: true }, role: { type: "string", required: true, references: { model: "roles", field: "_id" }, }, team: { type: "string", required: false, references: { model: "teams", field: "_id" }, }, }, }, emailAndPassword: { enabled: true }, advanced: { useSecureCookies: false, defaultCookieAttributes: { httpOnly: true, sameSite: "None", secure: false, maxAge: 1000000, }, }, session: { expiresIn: 60 * 60 * 24 * 7, cookieCache: { enabled: true, maxAge: 5 * 60 }, }, database: mongodbAdapter(db, { client: mongoClient }), });
๐Ÿง  Whatโ€™s happening

/auth/login works fine.

I get this response also sessioncreated :
{ "message": "Login successful", "data": { "redirect": true, "token": "RY7qleTD0SwUNggG9bBzp17hfMblIAs7", "url": "https://google.com", "user": { "id": "68f5ef166e37cd53cc1724be", "email": "john@gmail.com", "name": "John Doe", "emailVerified": false, "createdAt": "2025-10-20T08:13:10.192Z", "updatedAt": "2025-10-20T08:13:10.192Z" } } }
Was this page helpful?