[GH-ISSUE #8159] setCookieCache overwrites fresh account cookie with stale request data during OAuth re-login #11008

Open
opened 2026-04-13 07:23:15 -05:00 by GiteaMirror · 0 comments
Owner

Originally created by @arunanshub on GitHub (Feb 26, 2026).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/8159

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

Minimal Config

betterAuth({
	session: {
		cookieCache: { enabled: true, maxAge: 604800, strategy: "jwe" },
	},
	account: {
		storeStateStrategy: "cookie",
		storeAccountCookie: true,
	},
	plugins: [genericOAuth({ config: [auth0({ ... })] })],
});

Steps

  1. Log in via OAuth. Session and account cookies are set. Everything works.
  2. Wait for the OAuth access token to expire (or simulate by letting it lapse).
  3. The app redirects the user to the sign-in page. The old session and account cookies still exist in the browser.
  4. Log in again via OAuth.
  5. The callback completes, new session and account cookies are set but the account cookie contains the old tokens, not the new ones from the fresh OAuth exchange.
  6. Any API call using the access token from the account cookie fails because the token is stale/expired.
  7. Clearing all cookies and logging in again works, because there is no old account cookie for setCookieCache to read.

Current vs. Expected behavior

Current behavior

When a user with an existing (stale) session logs in again (e.g. after being redirected to the sign-in page due to an expired access token), the fresh OAuth tokens from the new login are lost. The account data cookie ends up containing the old/stale tokens from the previous session.

Expected Behavior

After a successful OAuth login, the account cookie should always contain the tokens from the new login, regardless of whether the browser had an old account cookie.

What version of Better Auth are you using?

1.4.18

System info

{
  "system": {
    "platform": "linux",
    "arch": "x64",
    "version": "#1 SMP PREEMPT_DYNAMIC Thu Jun  5 18:30:46 UTC 2025",
    "release": "6.6.87.2-microsoft-standard-WSL2",
    "cpuCount": 16,
    "cpuModel": "AMD Ryzen 7 7745HX with Radeon Graphics",
    "totalMemory": "15.22 GB",
    "freeMemory": "4.90 GB"
  },
  "node": {
    "version": "v25.6.1",
    "env": "development"
  },
  "packageManager": {
    "name": "pnpm",
    "version": "10.28.2"
  },
  "frameworks": [
    {
      "name": "next",
      "version": "16.1.6"
    },
    {
      "name": "react",
      "version": "19.2.4"
    }
  ],
  "databases": null,
  "betterAuth": {
    "version": "^1.4.18",
    "config": null
  }
}

Which area(s) are affected? (Select all that apply)

Client

Auth config (if applicable)

betterAuth({
  secret: env.AUTH0_SECRET || "dummy",
  baseURL: env.AUTH0_BASE_URL || "http://localhost:3000",

  session: {
    cookieCache: {
      enabled: true,
      maxAge: 7 * 24 * 60 * 60,
      strategy: "jwe",
      refreshCache: true,
    },
  },
  account: {
    storeStateStrategy: "cookie",
    storeAccountCookie: true,
  },

  onAPIError: {
    errorURL: "/auth/error",
  },

  advanced: {
    cookiePrefix: "my-auth-app",
  },

  plugins: [
    genericOAuth({
      config: [
        auth0({
          clientId: env.AUTH0_CLIENT_ID || "dummy",
          clientSecret: env.AUTH0_CLIENT_SECRET || "dummy",
          domain: urlToDomain(env.AUTH0_ISSUER_BASE_URL || "auth.myauth.io"),
          redirectURI: getRedirectURL(),
          scopes: ["openid", "profile", "email", "offline_access"],
        }),
      ],
    }),

    nextCookies(),
  ],
});

Additional context

No response

Originally created by @arunanshub on GitHub (Feb 26, 2026). Original GitHub issue: https://github.com/better-auth/better-auth/issues/8159 ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce ### Minimal Config ```tsx betterAuth({ session: { cookieCache: { enabled: true, maxAge: 604800, strategy: "jwe" }, }, account: { storeStateStrategy: "cookie", storeAccountCookie: true, }, plugins: [genericOAuth({ config: [auth0({ ... })] })], }); ``` ### Steps 1. Log in via OAuth. Session and account cookies are set. Everything works. 2. Wait for the OAuth access token to expire (or simulate by letting it lapse). 3. The app redirects the user to the sign-in page. The old session and account cookies still exist in the browser. 4. Log in again via OAuth. 5. The callback completes, new session and account cookies are set but the account cookie contains the old tokens, not the new ones from the fresh OAuth exchange. 6. Any API call using the access token from the account cookie fails because the token is stale/expired. 7. Clearing all cookies and logging in again works, because there is no old account cookie for `setCookieCache` to read. ### Current vs. Expected behavior ### Current behavior When a user with an existing (stale) session logs in again (e.g. after being redirected to the sign-in page due to an expired access token), the fresh OAuth tokens from the new login are lost. The account data cookie ends up containing the old/stale tokens from the previous session. ### Expected Behavior After a successful OAuth login, the account cookie should always contain the tokens from the new login, regardless of whether the browser had an old account cookie. ### What version of Better Auth are you using? 1.4.18 ### System info ```bash { "system": { "platform": "linux", "arch": "x64", "version": "#1 SMP PREEMPT_DYNAMIC Thu Jun 5 18:30:46 UTC 2025", "release": "6.6.87.2-microsoft-standard-WSL2", "cpuCount": 16, "cpuModel": "AMD Ryzen 7 7745HX with Radeon Graphics", "totalMemory": "15.22 GB", "freeMemory": "4.90 GB" }, "node": { "version": "v25.6.1", "env": "development" }, "packageManager": { "name": "pnpm", "version": "10.28.2" }, "frameworks": [ { "name": "next", "version": "16.1.6" }, { "name": "react", "version": "19.2.4" } ], "databases": null, "betterAuth": { "version": "^1.4.18", "config": null } } ``` ### Which area(s) are affected? (Select all that apply) Client ### Auth config (if applicable) ```typescript betterAuth({ secret: env.AUTH0_SECRET || "dummy", baseURL: env.AUTH0_BASE_URL || "http://localhost:3000", session: { cookieCache: { enabled: true, maxAge: 7 * 24 * 60 * 60, strategy: "jwe", refreshCache: true, }, }, account: { storeStateStrategy: "cookie", storeAccountCookie: true, }, onAPIError: { errorURL: "/auth/error", }, advanced: { cookiePrefix: "my-auth-app", }, plugins: [ genericOAuth({ config: [ auth0({ clientId: env.AUTH0_CLIENT_ID || "dummy", clientSecret: env.AUTH0_CLIENT_SECRET || "dummy", domain: urlToDomain(env.AUTH0_ISSUER_BASE_URL || "auth.myauth.io"), redirectURI: getRedirectURL(), scopes: ["openid", "profile", "email", "offline_access"], }), ], }), nextCookies(), ], }); ``` ### Additional context _No response_
GiteaMirror added the coreoauthbug labels 2026-04-13 07:23:15 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#11008