[GH-ISSUE #3642] Session Cookie Generation helper #26993

Closed
opened 2026-04-17 17:46:08 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @ghost2023 on GitHub (Jul 27, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/3642

Hey, I was wondering if there a way to generate session token for cookie to help with automation and automated testing.
And Thank you for this wonderful library.

Originally created by @ghost2023 on GitHub (Jul 27, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/3642 Hey, I was wondering if there a way to generate session token for cookie to help with automation and automated testing. And Thank you for this wonderful library.
GiteaMirror added the locked label 2026-04-17 17:46:08 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Jul 27, 2025):

Hi @ghost2023! I'm Dosu and I’m helping the better-auth team.

Yes, better-auth provides test utilities for exactly this purpose. You can use the signInWithTestUser or signInWithUser helpers to perform a real authentication with a test or custom user, and these will extract the session token from the set-cookie header for you. This gives you a valid session cookie that you can use in your automated tests, closely mirroring real authentication flows and maintaining security boundaries.

Here’s a quick example:

const { headers, session, user } = await signInWithTestUser();
// Now you can use `headers` (which includes the session cookie) in your automated requests

If you want to sign in with a specific user, use signInWithUser:

const { headers, res } = await signInWithUser('user@example.com', 'password123');

These helpers handle extracting the session token from the set-cookie header and storing it in a Headers object for you. There isn’t a public API for generating arbitrary session tokens outside the authentication flow; using these test utilities is the recommended and secure approach for automation and testing. For more advanced scenarios, internal helpers like parseSetCookieHeader and setCookieToHeader are used to manage cookies in headers during tests, but you usually won’t need to use them directly.
Reference: test-utils and test-instance source

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:3124166596 --> @dosubot[bot] commented on GitHub (Jul 27, 2025): <!-- Greeting --> Hi @ghost2023! I'm [Dosu](https://go.dosu.dev/dosubot) and I’m helping the better-auth team. <!-- Answer --> Yes, better-auth provides test utilities for exactly this purpose. You can use the signInWithTestUser or signInWithUser helpers to perform a real authentication with a test or custom user, and these will extract the session token from the set-cookie header for you. This gives you a valid session cookie that you can use in your automated tests, closely mirroring real authentication flows and maintaining security boundaries. Here’s a quick example: ```ts const { headers, session, user } = await signInWithTestUser(); // Now you can use `headers` (which includes the session cookie) in your automated requests ``` If you want to sign in with a specific user, use signInWithUser: ```ts const { headers, res } = await signInWithUser('user@example.com', 'password123'); ``` These helpers handle extracting the session token from the set-cookie header and storing it in a Headers object for you. There isn’t a public API for generating arbitrary session tokens outside the authentication flow; using these test utilities is the recommended and secure approach for automation and testing. For more advanced scenarios, internal helpers like parseSetCookieHeader and setCookieToHeader are used to manage cookies in headers during tests, but you usually won’t need to use them directly. [Reference: test-utils and test-instance source](https://github.com/better-auth/better-auth/blob/e7af3aa80c0cce284ed7ef3b61056322e50cd964/packages/better-auth/src/test-utils/test-instance.ts) <!-- 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/d296e1d2-0f23-4d42-aca3-7cdbe60aca70?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/d296e1d2-0f23-4d42-aca3-7cdbe60aca70?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/d296e1d2-0f23-4d42-aca3-7cdbe60aca70?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/d296e1d2-0f23-4d42-aca3-7cdbe60aca70?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/d296e1d2-0f23-4d42-aca3-7cdbe60aca70?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/d296e1d2-0f23-4d42-aca3-7cdbe60aca70?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/d296e1d2-0f23-4d42-aca3-7cdbe60aca70?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/3642)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#26993