GetSession not returning user and/or session objects, here’s details

UPDATE: signInEmail is successfully making database calls, but the getSession is not. It’s a svelte5/sveltekit project implementing authentication with better-auth. Route protection implemented in routes/+hooks.server.ts, routes/login/+page.server.ts is successfully logging in and creating a valid (I believe) cookie. Here’s code and logged results. My apologies for all the logs, I’m a novice at everything so just trying to work my way through this. Wish it worked like the better-auth documentation for svelte. See attached PDF for code and logs. I tried numerous approaches suggested by AI tools and people to extract data from the "body" of the response object but not successful. The <pending> in the response from getSession using response.json() makes me wonder if the database isn't responding but shouldn't the console.log not process given the "await" on the getSession. And, the signInEmail updates the database perfectly. auth.ts file export const auth = betterAuth({ database: drizzleAdapter(dbAUTH, { provider: 'sqlite', schema: { user, session, account, verification } }), session: { strategy: 'database', // storeSessionInDatabase: true, // preserveSessionInDatabase: true, secret: process.env.AUTH_SECRET, expiresIn: 30 * 24 * 60 * 60, freshAge: 60 * 5, cookieCache: { maxAge: 60 * 60 * 24 * 30, enabled: true }, cookie: { name: 'better-auth.session_token', // path: '/', // maxAge: 60 * 60 * 24 * 30, // 30 days secure: process.env.NODE_ENV === 'production', httpOnly: true, sameSite: 'lax' } }, emailAndPassword: { enabled: true, async onSignIn({ user, session }: { user: User; session: Session }) { console.log('auth - onSignIn - user', user, 'session', session); return { session, user }; } },
2 Replies
rtmorgan
rtmorgan5d ago
try removing the 'asResponse:true' in your +hooks.server.ts: const sessionResponse = await auth.api.getSession({ headers: event.request.headers, asResponse: true <--- remove this }); you don't need to get a Web API Response object here to get the session and user returned from getSession but rather just normal data returned from getSession will contain them
braveheartwilliam
@rtmorgan thanks. Already have tried both with and without with each suggested solution. The issue is something misaligned between signInEmail cookie and header and getSession validation that fails before interacting with the database. I now log database activity at sqlite3 and getSession doesn’t make any calls. Thanks for your help.

Did you find this page helpful?