5 Replies
Hi there! It sounds like your backend is returning a null session, which can happen for a few common reasons with Better Auth. One possibility is that the session cookie isn’t being preserved between requests. For example, if your frontend and backend domains differ, you need to ensure your CORS configuration is correctly set up to allow credentials (the session cookie) from your frontend domain [1]. 
Another common factor is the use (or lack) of cookie caching. There was an issue reported where if the cookieCache isn’t properly configured in your auth setup, then calling auth.api.getSession() can return null even when a session exists [5]. So please double-check that your auth configuration includes a proper session.cookieCache setting if you expect it to be used.
Could you confirm that:
1. Your CORS settings include the correct origins and allow credentials?
2. The session cookie is properly sent with your requests?
3. You have set up the cookieCache (if you need that feature) in your auth configuration?
If you’re still stuck after verifying these, feel free to tag @bekacru or @ping__ for more in-depth troubleshooting. Have you already checked these settings on your end?
Sources:
[1] Session is null when using diffrent frontend and backend
[5] auth.api.getSession always returns null if session.cookieCache is not set in the 
auth
— Better Auth is powered by https://clarm.comGitHub
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
Session is null when using diffrent frontend and backend · Issue #...
Is this suited for github? Yes, this is suited for github To Reproduce create a frontend using nextjs and react-query create a backend (with hono) set up cors app.use( "/*", cors({ origin...
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 ...
Why is my session in the backend giving null  am using express. js and cors front is separate from back @Better Auth
It sounds like you might be running into a few common pitfalls when using Express with a separate front end. Based on the context provided:
• There’s a known issue where even with a valid session token in the database, calling auth.api.getSession can return null if the configuration isn’t quite right [1].
• When your backend and frontend are hosted on different domains, proper CORS and cookie configuration become critical. Make sure that your CORS middleware is registered before your Better Auth routes, and that your cookie settings (such as SameSite, secure flag, and domain/path) are correctly configured for cross-domain requests [4][5].
• Additionally, ensure you’re correctly converting and passing your headers to auth.api.getSession (using functions like fromNodeHeaders) so that Better Auth can properly detect the session [1][5]. Can you share a bit more about your CORS or cookie setup? That might help us pinpoint the problem further. If the issue persists, you could tag @bekacru or @ping__ so they can assist you further. Sources: [1] auth.api.getSession() returns null despite valid session token in database and successful HTTP endpoint response [4] CORS Issue [5] Express Integration | Better Auth — Better Auth is powered by https://clarm.com
• When your backend and frontend are hosted on different domains, proper CORS and cookie configuration become critical. Make sure that your CORS middleware is registered before your Better Auth routes, and that your cookie settings (such as SameSite, secure flag, and domain/path) are correctly configured for cross-domain requests [4][5].
• Additionally, ensure you’re correctly converting and passing your headers to auth.api.getSession (using functions like fromNodeHeaders) so that Better Auth can properly detect the session [1][5]. Can you share a bit more about your CORS or cookie setup? That might help us pinpoint the problem further. If the issue persists, you could tag @bekacru or @ping__ so they can assist you further. Sources: [1] auth.api.getSession() returns null despite valid session token in database and successful HTTP endpoint response [4] CORS Issue [5] Express Integration | Better Auth — Better Auth is powered by https://clarm.com
GitHub
better-auth/better-auth
The most comprehensive authentication framework for TypeScript - better-auth/better-auth
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: Set up BetterAuth with Prisma adapter and MySQL database Configure Express server with BetterAuth routes at /api/auth/* Creat...
GitHub
CORS Issue · Issue #915 · better-auth/better-auth
Is this suited for github? Yes, this is suited for github To Reproduce Create express node backend and install better-auth Setup express const app = express(); const port = 3006; app.use(cors({ ori...
@Better Auth  what is betterauth looking for in the cors
Better Auth enforces strict CORS checks by ensuring that incoming requests originate from a trusted source. Essentially, it looks for the request’s origin header to be explicitly approved in your configuration via the trustedOrigins option. If the origin isn’t included (or doesn’t match a valid pattern, like a wildcard for subdomains), Better Auth will block the request to prevent unauthorized cross-origin access [2][5].
Are you experiencing any issues with dynamic origins in your development environment, or would you like some tips on configuring trusted origins for scenarios like Vercel preview deployments?
Sources:
[2] Better Auth Fastify Integration Guide | Better Auth
[5] Invalid Origin error with BetterAuth on Vercel preview deployments
— Better Auth is powered by https://clarm.com
Better Auth Fastify Integration Guide | Better Auth
Learn how to seamlessly integrate Better Auth with your Fastify application.
GitHub
Invalid Origin error with BetterAuth on Vercel preview deployments ...
Is this suited for github? Yes, this is suited for github To Reproduce Set up BetterAuth in a Next.js application Create a pull request that triggers a Vercel preview deployment Try to authenticate...