Clear cookies when account_data no longer accessible in stateless mode #3036

Open
opened 2026-03-13 10:35:39 -05:00 by GiteaMirror · 0 comments
Owner

Originally created by @stychu on GitHub (Mar 12, 2026).

That's a really sensible suggestion. You're right that the current multi-cookie setup with independent expiries creates a confusing state where you can have a valid session_data but expired account_data (or vice versa), leading to the "Account not found" errors.

Currently there's no automatic coordinated cleanup when one cookie expires—cleanup only happens on explicit deletion. There is logic to sync account_data when session_data refreshes, but it doesn't cover the expiration edge case you're describing.

Your proposal makes a lot of sense:

  • If account_data is gone and can't be recovered (stateless mode), the session is effectively dead anyway
  • Clearing all cookies would give the client a clean signal to trigger re-authentication (silent or redirect)
  • It removes the ambiguous half-authenticated state that causes confusing errors

There are also some related issues you might want to look at for context on the broader stateless cookie problems:

  • #7994 - updateAge doesn't extend session_token expiry in stateless mode
  • #8161 - Race condition where fresh cookies get overwritten with stale data during OAuth callback

If you're interested in contributing this fix, it could be implemented in the cookie validation/retrieval logic—detect when account_data is missing but other auth cookies exist, and clear them all. Would be a meaningful improvement to stateless mode reliability.

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other

Chat with Dosu Open in Cursor Join Discord Share on X

Originally posted by @dosubot in https://github.com/better-auth/better-auth/discussions/8287#discussioncomment-16032214

Originally created by @stychu on GitHub (Mar 12, 2026). <!-- Answer --> That's a really sensible suggestion. You're right that the current multi-cookie setup with independent expiries creates a confusing state where you can have a valid `session_data` but expired `account_data` (or vice versa), leading to the "Account not found" errors. Currently there's **no automatic coordinated cleanup** when one cookie expires—[cleanup only happens on explicit deletion](https://github.com/better-auth/better-auth/blob/7fefc2bbed2b66b2349ca182144b10eff171e345/packages/better-auth/src/cookies/index.ts#L328-L339). There *is* [logic to sync `account_data` when `session_data` refreshes](https://github.com/better-auth/better-auth/blob/7fefc2bbed2b66b2349ca182144b10eff171e345/packages/better-auth/src/cookies/index.ts#L256-L261), but it doesn't cover the expiration edge case you're describing. Your proposal makes a lot of sense: - If `account_data` is gone and can't be recovered (stateless mode), the session is effectively dead anyway - Clearing all cookies would give the client a clean signal to trigger re-authentication (silent or redirect) - It removes the ambiguous half-authenticated state that causes confusing errors There are also some related issues you might want to look at for context on the broader stateless cookie problems: - [#7994](https://github.com/better-auth/better-auth/issues/7994) - `updateAge` doesn't extend `session_token` expiry in stateless mode - [#8161](https://github.com/better-auth/better-auth/pull/8161) - Race condition where fresh cookies get overwritten with stale data during OAuth callback If you're interested in contributing this fix, it could be implemented in the cookie validation/retrieval logic—detect when `account_data` is missing but other auth cookies exist, and clear them all. Would be a meaningful improvement to stateless mode reliability. <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/87750b04-8581-4fcd-a8cc-7ed1ef4910e8?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/87750b04-8581-4fcd-a8cc-7ed1ef4910e8?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/87750b04-8581-4fcd-a8cc-7ed1ef4910e8?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/87750b04-8581-4fcd-a8cc-7ed1ef4910e8?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/87750b04-8581-4fcd-a8cc-7ed1ef4910e8?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/87750b04-8581-4fcd-a8cc-7ed1ef4910e8?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/87750b04-8581-4fcd-a8cc-7ed1ef4910e8?feedback_type=other)</sup> [![Chat with Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/cdda13d9-dd27-4d31-b09a-5d8bec92de21/ask?utm_source=github)&nbsp;[![Open in Cursor](https://dosu.dev/dosu-in-cursor.svg)](https://cursor.com/link/prompt?text=That%27s%20a%20really%20sensible%20suggestion.%20You%27re%20right%20that%20the%20current%20multi-cookie%20setup%20with%20independent%20expiries%20creates%20a%20confusing%20state%20where%20you%20can%20have%20a%20valid%20%60session_data%60%20but%20expired%20%60account_data%60%20%28or%20vice%20versa%29%2C%20leading%20to%20the%20%22Account%20not%20found%22%20errors.%0A%0ACurrently%20there%27s%20%2A%2Ano%20automatic%20coordinated%20cleanup%2A%2A%20when%20one%20cookie%20expires%E2%80%94%5Bcleanup%20only%20happens%20on%20explicit%20deletion%5D%28https%3A//github.com/better-auth/better-auth/blob/7fefc2bbed2b66b2349ca182144b10eff171e345/packages/better-auth/src/cookies/index.ts%23L328-L339%29.%20There%20%2Ais%2A%20%5Blogic%20to%20sync%20%60account_data%60%20when%20%60session_data%60%20refreshes%5D%28https%3A//github.com/better-auth/better-auth/blob/7fefc2bbed2b66b2349ca182144b10eff171e345/packages/better-auth/src/cookies/index.ts%23L256-L261%29%2C%20but%20it%20doesn%27t%20cover%20the%20expiration%20edge%20case%20you%27re%20describing.%0A%0AYour%20proposal%20makes%20a%20lot%20of%20sense%3A%0A-%20If%20%60account_data%60%20is%20gone%20and%20can%27t%20be%20recovered%20%28stateless%20mode%29%2C%20the%20session%20is%20effectively%20dead%20anyway%0A-%20Clearing%20all%20cookies%20would%20give%20the%20client%20a%20clean%20signal%20to%20trigger%20re-authentication%20%28silent%20or%20redirect%29%0A-%20It%20removes%20the%20ambiguous%20half-authenticated%20state%20that%20causes%20confusing%20errors%0A%0AThere%20are%20also%20some%20related%20issues%20you%20might%20want%20to%20look%20at%20for%20context%20on%20the%20broader%20stateless%20cookie%20problems%3A%0A-%20%5B%237994%5D%28https%3A//github.com/better-auth/better-auth/issues/7994%29%20-%20%60updateAge%60%20doesn%27t%20extend%20%60session_token%60%20expiry%20in%20stateless%20mode%0A-%20%5B%238161%5D%28https%3A//github.com/better-auth/better-auth/pull/8161%29%20-%20Race%20condition%20where%20fresh%20cookies%20get%20overwritten%20with%20stale%20data%20during%20OAuth%20callback%0A%0AIf%20you%27re%20interested%20in%20contributing%20this%20fix%2C%20it%20could%20be%20implemented%20in%20the%20cookie%20validation/retrieval%20logic%E2%80%94detect%20when%20%60account_data%60%20is%20missing%20but%20other%20auth%20cookies%20exist%2C%20and%20clear%20them%20all.%20Would%20be%20a%20meaningful%20improvement%20to%20stateless%20mode%20reliability.)&nbsp;[![Join Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share on X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/better-auth/better-auth/discussions/8287) _Originally posted by @dosubot in https://github.com/better-auth/better-auth/discussions/8287#discussioncomment-16032214_
GiteaMirror added the enhancement label 2026-03-13 10:35:39 -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#3036