[GH-ISSUE #1466] Automatically Set Cookie Domain from callbackURL (if in trustedOrigins) #8770

Closed
opened 2026-04-13 03:58:39 -05:00 by GiteaMirror · 2 comments
Owner

Originally created by @shura-v on GitHub (Feb 16, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/1466

Is this suited for github?

  • Yes, this is suited for github

Currently, better-auth requires manually setting crossSubDomainCookies.domain, which makes it difficult to support multiple environments (localhost, staging, production, etc.). This creates additional configuration overhead and makes it less flexible when using the same authentication setup across different domains.

Describe the solution you'd like

Instead of requiring a fixed domain in the config, better-auth could automatically extract the domain from callbackURL, but only if the domain is included in trustedOrigins. If the extracted domain is not trusted, it should fall back to a default or fail gracefully.

Describe alternatives you've considered

  • Manually setting the domain for each environment (e.g., localhost, staging, production). This works but requires additional configuration for each deployment.
  • Custom middleware to dynamically modify cookies before they are set. This would add unnecessary complexity when the library could handle it natively.

Additional context

No response

Originally created by @shura-v on GitHub (Feb 16, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/1466 ### Is this suited for github? - [x] Yes, this is suited for github ### Is your feature request related to a problem? Please describe. Currently, `better-auth` requires manually setting `crossSubDomainCookies.domain`, which makes it difficult to support multiple environments (localhost, staging, production, etc.). This creates additional configuration overhead and makes it less flexible when using the same authentication setup across different domains. ### Describe the solution you'd like Instead of requiring a fixed `domain` in the config, `better-auth` could **automatically extract the domain from `callbackURL`**, but only if the domain is included in `trustedOrigins`. If the extracted domain is not trusted, it should fall back to a default or fail gracefully. ### Describe alternatives you've considered - **Manually setting the `domain`** for each environment (e.g., `localhost`, `staging`, `production`). This works but requires additional configuration for each deployment. - **Custom middleware to dynamically modify cookies** before they are set. This would add unnecessary complexity when the library could handle it natively. ### Additional context _No response_
GiteaMirror added the locked label 2026-04-13 03:58:39 -05:00
Author
Owner

@kayotimoteo commented on GitHub (May 16, 2025):

That would be an interesting feature.

<!-- gh-comment-id:2887370322 --> @kayotimoteo commented on GitHub (May 16, 2025): That would be an interesting feature.
Author
Owner

@dosubot[bot] commented on GitHub (Aug 15, 2025):

Hi, @shura-v. I'm Dosu, and I'm helping the better-auth team manage their backlog and am marking this issue as stale.

Issue Summary:

  • You suggested enhancing better-auth to automatically set the cookie domain by extracting it from the callbackURL when the domain is in trustedOrigins.
  • This aims to simplify configuration across environments by removing the need for manual domain setting or custom middleware.
  • I found the idea interesting but there have been no further updates or progress on this feature.
  • The issue remains unresolved and open for discussion.

Next Steps:

  • Please let me know if this feature is still relevant to the latest version of better-auth by commenting on this issue.
  • If I don’t hear back within 7 days, the issue will be automatically closed to keep the backlog manageable.

Thanks for your understanding and contribution!

<!-- gh-comment-id:3192023123 --> @dosubot[bot] commented on GitHub (Aug 15, 2025): Hi, @shura-v. I'm [Dosu](https://dosu.dev), and I'm helping the better-auth team manage their backlog and am marking this issue as stale. **Issue Summary:** - You suggested enhancing better-auth to automatically set the cookie domain by extracting it from the callbackURL when the domain is in trustedOrigins. - This aims to simplify configuration across environments by removing the need for manual domain setting or custom middleware. - I found the idea interesting but there have been no further updates or progress on this feature. - The issue remains unresolved and open for discussion. **Next Steps:** - Please let me know if this feature is still relevant to the latest version of better-auth by commenting on this issue. - If I don’t hear back within 7 days, the issue will be automatically closed to keep the backlog manageable. Thanks for your understanding and contribution!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#8770