[GH-ISSUE #8911] Add guardrails for cookie integration plugin order to prevent silent missing Set-Cookie #11235

Open
opened 2026-04-13 07:34:50 -05:00 by GiteaMirror · 2 comments
Owner

Originally created by @h30s on GitHub (Apr 2, 2026).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/8911

Originally assigned to: @ping-maxwell on GitHub.

Is this suited for github?

  • Yes, this is suited for github

Is your feature request related to a problem? Please describe.

Yes.

When using Better Auth, cookie integration plugins (like nextCookies, tanstackStartCookies, etc.) must be placed last in the plugins[] array.

If they are not last, any cookies added by plugins that run after them are not forwarded to the framework cookie store. This leads to silent failures like:

  • session not persisting
  • 2FA breaking (INVALID_*_COOKIES)
  • null sessions or redirect loops

This behavior is not obvious and no warning/error is shown, making it hard to debug.

Describe the solution you'd like

Add a guardrail during betterAuth() initialization:

  • Detect cookie integration plugins (by plugin.id)
  • If they are not the last plugin:
    • show a clear warning (or throw in dev mode)

Example message:

Cookie integration plugins should be last. Otherwise some Set-Cookie headers may be silently dropped.

This would prevent common misconfiguration and improve DX.

Describe alternatives you've considered

A more robust solution would be to make execution order independent by ensuring cookie plugins always run last internally (e.g., via hook finalization or priority system).

But a warning-based guardrail is simpler and a good first step.

Additional context

This issue has come up multiple times where the fix was simply "move the cookie plugin to the end".

Adding a guardrail would prevent this recurring confusion and improve reliability of core auth flows.

Happy to open a PR if this approach sounds good 👍

Originally created by @h30s on GitHub (Apr 2, 2026). Original GitHub issue: https://github.com/better-auth/better-auth/issues/8911 Originally assigned to: @ping-maxwell on GitHub. ### Is this suited for github? - [x] Yes, this is suited for github ### Is your feature request related to a problem? Please describe. Yes. When using Better Auth, cookie integration plugins (like `nextCookies`, `tanstackStartCookies`, etc.) must be placed last in the `plugins[]` array. If they are not last, any cookies added by plugins that run after them are not forwarded to the framework cookie store. This leads to silent failures like: - session not persisting - 2FA breaking (INVALID_*_COOKIES) - null sessions or redirect loops This behavior is not obvious and no warning/error is shown, making it hard to debug. ### Describe the solution you'd like Add a guardrail during `betterAuth()` initialization: - Detect cookie integration plugins (by `plugin.id`) - If they are not the last plugin: - show a clear warning (or throw in dev mode) Example message: > Cookie integration plugins should be last. Otherwise some Set-Cookie headers may be silently dropped. This would prevent common misconfiguration and improve DX. ### Describe alternatives you've considered A more robust solution would be to make execution order independent by ensuring cookie plugins always run last internally (e.g., via hook finalization or priority system). But a warning-based guardrail is simpler and a good first step. ### Additional context This issue has come up multiple times where the fix was simply "move the cookie plugin to the end". Adding a guardrail would prevent this recurring confusion and improve reliability of core auth flows. Happy to open a PR if this approach sounds good 👍
GiteaMirror added the awaiting external contributorenhancement labels 2026-04-13 07:34:51 -05:00
Author
Owner

@h30s commented on GitHub (Apr 5, 2026):

Hey @ping-maxwell Can I pick this up and open a PR?

<!-- gh-comment-id:4188897181 --> @h30s commented on GitHub (Apr 5, 2026): Hey @ping-maxwell Can I pick this up and open a PR?
Author
Owner

@ping-maxwell commented on GitHub (Apr 9, 2026):

Hey @ping-maxwell Can I pick this up and open a PR?

go for it!

<!-- gh-comment-id:4214305077 --> @ping-maxwell commented on GitHub (Apr 9, 2026): > Hey [@ping-maxwell](https://github.com/ping-maxwell) Can I pick this up and open a PR? go for it!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#11235