From 9fed2ff3ad2de9bb074f1ec3cb5756d1cd6c2cbf Mon Sep 17 00:00:00 2001 From: Bereket Engida <86073083+Bekacru@users.noreply.github.com> Date: Sat, 13 Dec 2025 15:23:03 -0800 Subject: [PATCH] fix: prevent stateless refresh with database configured (#6700) --- .../src/api/routes/session-api.test.ts | 9 ++-- .../better-auth/src/api/routes/session.ts | 5 +- .../src/context/create-context.test.ts | 52 +++++++++++++++++++ .../better-auth/src/context/create-context.ts | 11 ++++ 4 files changed, 73 insertions(+), 4 deletions(-) diff --git a/packages/better-auth/src/api/routes/session-api.test.ts b/packages/better-auth/src/api/routes/session-api.test.ts index 9e9b291873..a5f5f5b3c3 100644 --- a/packages/better-auth/src/api/routes/session-api.test.ts +++ b/packages/better-auth/src/api/routes/session-api.test.ts @@ -913,7 +913,7 @@ describe("cookie cache refreshCache", async () => { expect(fn).toHaveBeenCalledTimes(1); }); - it("should refresh cache stateless when refreshCache threshold is exceeded", async () => { + it("should not perform stateless refresh when a database is configured", async () => { const callsBefore = fn.mock.calls.length; vi.useFakeTimers(); @@ -930,9 +930,10 @@ describe("cookie cache refreshCache", async () => { }); expect(session.data).not.toBeNull(); - // With stateless refresh, no DB call should be made (it just refreshes the cookie) + // With a database configured, `refreshCache` is ignored (a warning is logged), + // so no additional DB call should be made here. const callsAfterRefresh = fn.mock.calls.length; - expect(callsAfterRefresh).toBe(callsBefore); // No DB call for stateless refresh + expect(callsAfterRefresh).toBe(callsBefore); await client.getSession({ fetchOptions: { @@ -1057,6 +1058,8 @@ describe("cookie cache refreshCache", async () => { it("should work without database when refreshCache threshold is reached", async () => { const { client, testUser, cookieSetter, auth } = await getTestInstance({ + // True stateless mode: no database configured + database: undefined as any, session: { cookieCache: { enabled: true, diff --git a/packages/better-auth/src/api/routes/session.ts b/packages/better-auth/src/api/routes/session.ts index 2766845762..f102dfcf7c 100644 --- a/packages/better-auth/src/api/routes/session.ts +++ b/packages/better-auth/src/api/routes/session.ts @@ -226,9 +226,12 @@ export const getSession =