[GH-ISSUE #5447] Finding a way to set the session cookie manually #10247

Closed
opened 2026-04-13 06:14:35 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @janwolfram on GitHub (Oct 21, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/5447

Hey I want to restore a session by setting the cookie manually. Is there any good practice to do this.
I tried writing my own plugin and using ctx methods like createAuthCookie or setNewSession but it did not work. Anyone can help here :)

Originally created by @janwolfram on GitHub (Oct 21, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/5447 Hey I want to restore a session by setting the cookie manually. Is there any good practice to do this. I tried writing my own plugin and using ctx methods like **createAuthCookie** or **setNewSession** but it did not work. Anyone can help here :)
GiteaMirror added the locked label 2026-04-13 06:14:35 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Oct 21, 2025):

Manually restoring a session by setting the cookie in Better Auth is tricky and not officially supported. The session cookie must be properly formatted and signed, matching Better Auth’s internal logic and security attributes. If you just set the cookie with ctx.createAuthCookie or ctx.setNewSession, it often won’t work unless the session state in the database and the cookie are perfectly aligned—this is a common pitfall and can break downstream flows (source).

The recommended approach is to use the asResponse: true or returnHeaders option on API endpoints (like signInEmail or signinAnonymous), which gives you the full Set-Cookie header to forward to the client (source, source). If you need to set the cookie manually, you must replicate the internal logic: use the correct cookie name, sign the value if required, and set all attributes (secure, httpOnly, sameSite, path) to match your config and browser policies (source). Manual construction is error-prone and may break if Better Auth updates its internals.

If you’re building a custom plugin, make sure you’re also updating the session in the database and using ctx.setSignedCookie with the right secret and attributes. Timing issues (like setting the cookie after session validation fails) can also cause problems. For most cases, letting Better Auth handle cookie setting via its API is safest.

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other  Chat with Dosu Join Discord Share on X

<!-- gh-comment-id:3425106287 --> @dosubot[bot] commented on GitHub (Oct 21, 2025): <!-- Answer --> Manually restoring a session by setting the cookie in Better Auth is tricky and not officially supported. The session cookie must be properly formatted and signed, matching Better Auth’s internal logic and security attributes. If you just set the cookie with ctx.createAuthCookie or ctx.setNewSession, it often won’t work unless the session state in the database and the cookie are perfectly aligned—this is a common pitfall and can break downstream flows ([source](https://github.com/better-auth/better-auth/issues/4585)). The recommended approach is to use the asResponse: true or returnHeaders option on API endpoints (like signInEmail or signinAnonymous), which gives you the full Set-Cookie header to forward to the client ([source](https://github.com/better-auth/better-auth/issues/3851), [source](https://github.com/better-auth/better-auth/issues/5266)). If you need to set the cookie manually, you must replicate the internal logic: use the correct cookie name, sign the value if required, and set all attributes (secure, httpOnly, sameSite, path) to match your config and browser policies ([source](https://github.com/better-auth/better-auth/issues/1813)). Manual construction is error-prone and may break if Better Auth updates its internals. If you’re building a custom plugin, make sure you’re also updating the session in the database and using ctx.setSignedCookie with the right secret and attributes. Timing issues (like setting the cookie after session validation fails) can also cause problems. For most cases, letting Better Auth handle cookie setting via its API is safest. <!-- 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/e5e12484-9c54-43ee-9860-12da912c1f62?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/e5e12484-9c54-43ee-9860-12da912c1f62?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/e5e12484-9c54-43ee-9860-12da912c1f62?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/e5e12484-9c54-43ee-9860-12da912c1f62?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/e5e12484-9c54-43ee-9860-12da912c1f62?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/e5e12484-9c54-43ee-9860-12da912c1f62?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/e5e12484-9c54-43ee-9860-12da912c1f62?feedback_type=other)</sup>&nbsp;&nbsp;[![Chat with Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/cdda13d9-dd27-4d31-b09a-5d8bec92de21/ask?utm_source=github)&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/issues/5447)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#10247