Adding test helpers to create a cookie more easily #2026

Closed
opened 2026-03-13 09:21:39 -05:00 by GiteaMirror · 5 comments
Owner

Originally created by @LJ1001 on GitHub (Sep 27, 2025).

Is this suited for github?

  • Yes, this is suited for github

First of all, thank you for this amazing project!

Maybe I am oversseing something, but searching in the docs and in the GH issues did not point me to a solution for the following issue.

I am writing tests for my API (which is protected by better-auth). Right now, whenever I want to perform authenticated requests, I am using the "/sign-up/email" endpoint to create a user for that particular test, followed by the "/sign-in/email" endpoint to get a cookie for that specific user which I am utilizing for all subsequent requests in my test.
This works fine - however, it adds a lot of overhead to my tests, because of the "expensive" password hashing algorithms. It adds up approximately 100-150ms per test suite if I create a user once per suite.

Any alternative approaches are welcome.
Thank you!

Describe the solution you'd like

It would be nice if there was a "testing"/"shortcut" function which skips the password hashing or at least the cookie generation to improve testability!

E.g.

const {user, session} = createTestUser()
const cookie = createCookie(session.token)

Describe alternatives you've considered

Regarding the user creation, I already have a workaround, I directly inject the user/account/session entries into the databse. For the cookie generation though, I did not find an easy solution. I went down the rabbit hole and tried to create it myself (copying the better-stack functionality), but I think I also have to update the cookie storage somehow.

Additional context

No response

Originally created by @LJ1001 on GitHub (Sep 27, 2025). ### Is this suited for github? - [x] Yes, this is suited for github ### Is your feature request related to a problem? Please describe. First of all, thank you for this amazing project! Maybe I am oversseing something, but searching in the docs and in the GH issues did not point me to a solution for the following issue. I am writing tests for my API (which is protected by better-auth). Right now, whenever I want to perform authenticated requests, I am using the `"/sign-up/email"` endpoint to create a user for that particular test, followed by the `"/sign-in/email"` endpoint to get a cookie for that specific user which I am utilizing for all subsequent requests in my test. This works fine - however, it adds a lot of overhead to my tests, because of the "expensive" password hashing algorithms. It adds up approximately 100-150ms per test suite if I create a user *once* per suite. Any alternative approaches are welcome. Thank you! ### Describe the solution you'd like It would be nice if there was a "testing"/"shortcut" function which skips the password hashing or at least the cookie generation to improve testability! E.g. ```javascript const {user, session} = createTestUser() const cookie = createCookie(session.token) ``` ### Describe alternatives you've considered Regarding the user creation, I already have a workaround, I directly inject the `user`/`account`/`session` entries into the databse. For the cookie generation though, I did not find an easy solution. I went down the rabbit hole and tried to create it myself (copying the `better-stack` functionality), but I think I also have to update the cookie storage somehow. ### Additional context _No response_
GiteaMirror added the enhancement label 2026-03-13 09:21:39 -05:00
Author
Owner

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

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

You can skip the expensive sign-in flow in your tests by directly creating a session in your database and then using the authClient.getCookie() function to generate the correct authentication cookie string. This helper ensures the cookie is properly signed and encoded, which is required for Better Auth to recognize the session—using a raw session token in the cookie header will not work. This approach is supported and used in integration tests for better-auth, making it both efficient and idiomatic for test setups where you want to avoid password hashing overhead. You can find more details about this pattern here and here.

To reply, just mention @dosu.


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

@dosubot[bot] commented on GitHub (Sep 27, 2025): <!-- Greeting --> Hi @LJ1001! I'm [Dosu](https://go.dosu.dev/dosubot) and I’m helping the better-auth team. <!-- Answer --> You can skip the expensive sign-in flow in your tests by directly creating a session in your database and then using the `authClient.getCookie()` function to generate the correct authentication cookie string. This helper ensures the cookie is properly signed and encoded, which is required for Better Auth to recognize the session—using a raw session token in the cookie header will not work. This approach is supported and used in integration tests for better-auth, making it both efficient and idiomatic for test setups where you want to avoid password hashing overhead. You can find more details about this pattern [here](https://github.com/better-auth/better-auth/issues/4517) and [here](https://github.com/better-auth/better-auth/issues/3892). <!-- 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/42c6b143-992e-4e30-9059-ad1965717e66?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/42c6b143-992e-4e30-9059-ad1965717e66?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/42c6b143-992e-4e30-9059-ad1965717e66?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/42c6b143-992e-4e30-9059-ad1965717e66?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/42c6b143-992e-4e30-9059-ad1965717e66?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/42c6b143-992e-4e30-9059-ad1965717e66?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/42c6b143-992e-4e30-9059-ad1965717e66?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/4940)
Author
Owner

@LJ1001 commented on GitHub (Sep 27, 2025):

Hi @dosu
Are you sure I can simply get a cookie. I cannot find any documentation about authClient.getCookie(). The only method I can see is authClient.getSession()which seems to be incorrect. Could you provide a minimal example on how to use the getCookie() method in a test where I want to have an authenicated user like so:

const cookie = // how to obtain a valid cookie for a user
const res = await app.request("/my/protected/endpoint", { headers: { cookie } })
expect(res.status).toBe(200)
@LJ1001 commented on GitHub (Sep 27, 2025): Hi @dosu Are you sure I can simply get a cookie. I cannot find any documentation about `authClient.getCookie()`. The only method I can see is `authClient.getSession()`which seems to be incorrect. Could you provide a minimal example on how to use the `getCookie()` method in a test where I want to have an authenicated user like so: ```javascript const cookie = // how to obtain a valid cookie for a user const res = await app.request("/my/protected/endpoint", { headers: { cookie } }) expect(res.status).toBe(200) ```
Author
Owner

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

You're right—authClient.getCookie() does not exist in the latest versions of Better Auth, which is why you can't find it in the docs or API. The current, supported way to get a valid authentication cookie for tests is to use the sign-in API with returnHeaders: true, then extract the Set-Cookie header from the response and use it in your test requests. Here’s a minimal example:

// Sign in and get the Set-Cookie header
const signInRes = await auth.api.signInEmail({
  body: { email: testUser.email, password: testUser.password },
  returnHeaders: true,
});
const cookie = signInRes.headers.getSetCookie()[0];

// Use the cookie in your test request
const res = await app.request("/my/protected/endpoint", {
  headers: { cookie },
});
expect(res.status).toBe(200);

If you’re bypassing the API and injecting sessions directly, you’ll need to replicate Better Auth’s cookie signing logic, which is non-trivial since the cookie must be signed and URL-encoded—just setting a raw session token won’t work examplediscussion.

Let me know if you need details on manual cookie signing or want to see how to do this without the sign-in API!

To reply, just mention @dosu.


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

@dosubot[bot] commented on GitHub (Sep 27, 2025): <!-- Answer --> You're right—`authClient.getCookie()` does not exist in the latest versions of Better Auth, which is why you can't find it in the docs or API. The current, supported way to get a valid authentication cookie for tests is to use the sign-in API with `returnHeaders: true`, then extract the `Set-Cookie` header from the response and use it in your test requests. Here’s a minimal example: ```js // Sign in and get the Set-Cookie header const signInRes = await auth.api.signInEmail({ body: { email: testUser.email, password: testUser.password }, returnHeaders: true, }); const cookie = signInRes.headers.getSetCookie()[0]; // Use the cookie in your test request const res = await app.request("/my/protected/endpoint", { headers: { cookie }, }); expect(res.status).toBe(200); ``` If you’re bypassing the API and injecting sessions directly, you’ll need to replicate Better Auth’s cookie signing logic, which is non-trivial since the cookie must be signed and URL-encoded—just setting a raw session token won’t work [example](https://github.com/better-auth/better-auth/issues/3892), [discussion](https://github.com/better-auth/better-auth/discussions/4684#discussioncomment-14408578). Let me know if you need details on manual cookie signing or want to see how to do this without the sign-in API! <!-- 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/46b4e3c6-9ce6-47ec-9191-0edecc67767e?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/46b4e3c6-9ce6-47ec-9191-0edecc67767e?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/46b4e3c6-9ce6-47ec-9191-0edecc67767e?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/46b4e3c6-9ce6-47ec-9191-0edecc67767e?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/46b4e3c6-9ce6-47ec-9191-0edecc67767e?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/46b4e3c6-9ce6-47ec-9191-0edecc67767e?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/46b4e3c6-9ce6-47ec-9191-0edecc67767e?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/4940)
Author
Owner

@Bekacru commented on GitHub (Sep 30, 2025):

You can pass custom password hashing function in you auth config and you can use plain text or something simpler.

@Bekacru commented on GitHub (Sep 30, 2025): You can pass custom password hashing function in you auth config and you can use plain text or something simpler.
Author
Owner

@nikolamilovic-fm commented on GitHub (Oct 29, 2025):

Would love to see an example of this @Bekacru , and would be great to have in the docs

@nikolamilovic-fm commented on GitHub (Oct 29, 2025): Would love to see an example of this @Bekacru , and would be great to have in the docs
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#2026