[PR #8161] fix: preserve fresh account cookie set during OAuth callback #24688

Open
opened 2026-04-15 22:30:50 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/8161
Author: @arunanshub
Created: 2/26/2026
Status: 🔄 Open

Base: mainHead: fix/stale-account-cookie-overwrite


📝 Commits (10+)

  • 764b957 fix: prevent setCookieCache from overwriting fresh account cookie
  • 8f71359 Merge branch 'canary' into fix/stale-account-cookie-overwrite
  • 201261c fix: make responseHeaders optional due to typescript errors and explain
  • ed1758d chore: formatting
  • a50a952 Merge branch 'canary' into fix/stale-account-cookie-overwrite
  • ea37e11 Merge branch 'canary' into fix/stale-account-cookie-overwrite
  • d1a2a3e Merge branch 'canary' into fix/stale-account-cookie-overwrite
  • a6a84d1 Merge remote-tracking branch 'origin/fix/stale-account-cookie-overwrite' into fix/stale-account-cookie-overwrite
  • 9eaf709 lint fix
  • 749e1bf Merge branch 'canary' into fix/stale-account-cookie-overwrite

📊 Changes

3 files changed (+192 additions, -5 deletions)

View changed files

📝 packages/better-auth/src/api/routes/account.test.ts (+148 -0)
📝 packages/better-auth/src/cookies/index.ts (+20 -4)
📝 packages/core/src/types/context.ts (+24 -1)

📄 Description

Problem

When storeAccountCookie is enabled, the OAuth callback flow sets a fresh account cookie (with new tokens from the provider) in handleOAuthUserInfo. Immediately after, setSessionCookie calls setCookieCache, which reads the account cookie from the incoming request (stale data) and writes it back, overwriting the fresh cookie.

This means users end up with expired OAuth tokens after every re-login unless they fully clear their cookies first.

Root cause (src/cookies/index.ts lines 255-261)

84565ccc2a/packages/better-auth/src/cookies/index.ts (L255-L261)

getAccountCookie reads from the incoming request via ctx.getCookie/ctx.headers. It has no awareness of cookies already set in the current response.

Fix

Check ctx.responseHeaders (the better-call response accumulator that ctx.setCookie appends to) before deciding whether to refresh the account cookie. If the account cookie was already set in this response, skip the refresh since the fresh data is already there.

Also adds responseHeaders attribute to GenericEndpointContext in @better-auth/core, since this property exists at runtime (from better-call's createInternalContext) but was not exposed on the public type.

Fixes #8159


Summary by cubic

Prevents stale request data from overwriting the fresh account cookie during OAuth callbacks, so users keep new tokens after re-login. Fixes #8159.

  • Bug Fixes

    • In setCookieCache, skip account cookie refresh if it was already set in this response (detected via Set-Cookie on ctx.responseHeaders). Session refresh behavior is unchanged.
    • Added a re-login test that decrypts the cookie and verifies getAccessToken returns the fresh token.
  • Refactors

    • Exposed optional responseHeaders?: Headers on GenericEndpointContext and used it for the in-response cookie check.

Written for commit e86886826d. Summary will update on new commits.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/better-auth/better-auth/pull/8161 **Author:** [@arunanshub](https://github.com/arunanshub) **Created:** 2/26/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/stale-account-cookie-overwrite` --- ### 📝 Commits (10+) - [`764b957`](https://github.com/better-auth/better-auth/commit/764b957631da283be8c01486fc4770df63b22559) fix: prevent setCookieCache from overwriting fresh account cookie - [`8f71359`](https://github.com/better-auth/better-auth/commit/8f71359afa9be6d85a9768ef055b0d4ebd8d3bc2) Merge branch 'canary' into fix/stale-account-cookie-overwrite - [`201261c`](https://github.com/better-auth/better-auth/commit/201261c1639afafde4842e936a1305925372c597) fix: make responseHeaders optional due to typescript errors and explain - [`ed1758d`](https://github.com/better-auth/better-auth/commit/ed1758dfc78296135f1315448f724823c732ce8d) chore: formatting - [`a50a952`](https://github.com/better-auth/better-auth/commit/a50a952450a71ab74dfbecad3d8aade6008007bb) Merge branch 'canary' into fix/stale-account-cookie-overwrite - [`ea37e11`](https://github.com/better-auth/better-auth/commit/ea37e118e6bb2988544384cfe6cf293e9f9f0946) Merge branch 'canary' into fix/stale-account-cookie-overwrite - [`d1a2a3e`](https://github.com/better-auth/better-auth/commit/d1a2a3ee46cabab1b4ddbbe23d49bf62c4a52c2f) Merge branch 'canary' into fix/stale-account-cookie-overwrite - [`a6a84d1`](https://github.com/better-auth/better-auth/commit/a6a84d15bd07830e8a736c3701f75b80a6351a77) Merge remote-tracking branch 'origin/fix/stale-account-cookie-overwrite' into fix/stale-account-cookie-overwrite - [`9eaf709`](https://github.com/better-auth/better-auth/commit/9eaf70912a8daa0ba28c677a7628a0525a185f34) lint fix - [`749e1bf`](https://github.com/better-auth/better-auth/commit/749e1bfbf6a4c174f2015d4ec7f06c61192e4440) Merge branch 'canary' into fix/stale-account-cookie-overwrite ### 📊 Changes **3 files changed** (+192 additions, -5 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/api/routes/account.test.ts` (+148 -0) 📝 `packages/better-auth/src/cookies/index.ts` (+20 -4) 📝 `packages/core/src/types/context.ts` (+24 -1) </details> ### 📄 Description ## Problem When `storeAccountCookie` is enabled, the OAuth callback flow sets a fresh account cookie (with new tokens from the provider) in `handleOAuthUserInfo`. Immediately after, `setSessionCookie` calls [`setCookieCache`](https://github.com/better-auth/better-auth/blob/84565ccc2a6bc0ae704d290028691adc7e9a4957/packages/better-auth/src/cookies/index.ts#L147), which reads the account cookie from the incoming request (stale data) and writes it back, overwriting the fresh cookie. This means users end up with expired OAuth tokens after every re-login unless they fully clear their cookies first. Root cause (`src/cookies/index.ts` lines 255-261) https://github.com/better-auth/better-auth/blob/84565ccc2a6bc0ae704d290028691adc7e9a4957/packages/better-auth/src/cookies/index.ts#L255-L261 `getAccountCookie` reads from the incoming request via `ctx.getCookie`/`ctx.headers`. It has no awareness of cookies already set in the current response. ## Fix Check `ctx.responseHeaders` (the `better-call` response accumulator that `ctx.setCookie` appends to) before deciding whether to refresh the account cookie. If the account cookie was already set in this response, skip the refresh since the fresh data is already there. Also adds `responseHeaders` attribute to `GenericEndpointContext` in `@better-auth/core`, since this property exists at runtime (from better-call's `createInternalContext`) but was not exposed on the public type. Fixes #8159 <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Prevents stale request data from overwriting the fresh account cookie during OAuth callbacks, so users keep new tokens after re-login. Fixes #8159. - **Bug Fixes** - In setCookieCache, skip account cookie refresh if it was already set in this response (detected via Set-Cookie on ctx.responseHeaders). Session refresh behavior is unchanged. - Added a re-login test that decrypts the cookie and verifies getAccessToken returns the fresh token. - **Refactors** - Exposed optional `responseHeaders?: Headers` on `GenericEndpointContext` and used it for the in-response cookie check. <sup>Written for commit e86886826d6b6756395fdfd49e8d64563438d4b0. Summary will update on new commits.</sup> <!-- End of auto-generated description by cubic. --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-04-15 22:30:50 -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#24688