[PR #5837] fix: override of cookiePrefix in getCookieCache function #6264

Open
opened 2026-03-13 12:52:49 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/5837
Author: @SignoreOladayo
Created: 11/7/2025
Status: 🔄 Open

Base: canaryHead: fix/support-cookieName-without-prefix-in-getCookieCahce


📝 Commits (2)

  • 88b556f fix: override of cookiePrefix in getCookieCache function
  • aa144b3 remove debug code

📊 Changes

2 files changed (+103 additions, -6 deletions)

View changed files

📝 packages/better-auth/src/cookies/cookies.test.ts (+89 -0)
📝 packages/better-auth/src/cookies/index.ts (+14 -6)

📄 Description

Problem

Given a betterAuth config like so;

betterAuth({
  ... // other config options,
  advanced: {
    cookiePrefix: 'some_prefix',
    cookies: {
      session_data: {
        name: "custom_cookie_name"
      }
    }
  }
})

The cookie name will be created as custom_cookie_name, which is expected by design.

When retrieving the cookie with getCookieCache like so;

getCookieCache(headers, {
  cookieName: "custom_cookie_name"
});

the cookie name gets prefixed with the default cookiePrefix, better-auth.custom_cookie_name, there by making the function return null, even if the cookie exists.
Also, passing an empty cookiePrefix value to the getCookieCache function doesn't help, as the function tries to find the cookie with .custom_cookie_name.

Proposed solution
Ensure the getCookiecache searches by only the cookieName if cookiePrefix is set as an empty string.

Clarification needed
I see the getSessionCookie function can have either cookiePrefix- or cookiePrefix.. I checked the betterAuth initialization code and couldn't find a place where the cookie name could be concatenated with a -.
Is this different concatenator in getSessionCookie by design? Otherwise, the change done in the getCookieCache can be reused there as well.


Summary by cubic

Fix getCookieCache to respect custom cookie names without a prefix. When cookiePrefix is an empty string, it now searches using only cookieName, preventing null cache results.

  • Bug Fixes
    • Skip applying the default prefix when cookiePrefix is "" and preserve __Secure cookie handling.
    • Added tests for custom cookie names without a prefix and for wrong-prefix lookups returning null.

Written for commit aa144b3aba. Summary will update automatically on new commits.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/better-auth/better-auth/pull/5837 **Author:** [@SignoreOladayo](https://github.com/SignoreOladayo) **Created:** 11/7/2025 **Status:** 🔄 Open **Base:** `canary` ← **Head:** `fix/support-cookieName-without-prefix-in-getCookieCahce` --- ### 📝 Commits (2) - [`88b556f`](https://github.com/better-auth/better-auth/commit/88b556fcc1e5cbe5366d458771972818b4a70829) fix: override of cookiePrefix in getCookieCache function - [`aa144b3`](https://github.com/better-auth/better-auth/commit/aa144b3aba222ca9027bd24372884712eddc3d92) remove debug code ### 📊 Changes **2 files changed** (+103 additions, -6 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/cookies/cookies.test.ts` (+89 -0) 📝 `packages/better-auth/src/cookies/index.ts` (+14 -6) </details> ### 📄 Description **Problem** Given a `betterAuth` config like so; ``` betterAuth({ ... // other config options, advanced: { cookiePrefix: 'some_prefix', cookies: { session_data: { name: "custom_cookie_name" } } } }) ``` The cookie name will be created as `custom_cookie_name`, which is expected by design. When retrieving the cookie with `getCookieCache` like so; ``` getCookieCache(headers, { cookieName: "custom_cookie_name" }); ``` the cookie name gets prefixed with the default cookiePrefix, `better-auth.custom_cookie_name`, there by making the function return null, even if the cookie exists. Also, passing an empty `cookiePrefix` value to the `getCookieCache` function doesn't help, as the function tries to find the cookie with `.custom_cookie_name`. **Proposed solution** Ensure the `getCookiecache` searches by only the cookieName if `cookiePrefix` is set as an empty string. **Clarification needed** I see the `getSessionCookie` function can have either `cookiePrefix-` or `cookiePrefix.`. I checked the `betterAuth` initialization code and couldn't find a place where the cookie name could be concatenated with a `-`. Is this different concatenator in `getSessionCookie` by design? Otherwise, the change done in the getCookieCache can be reused there as well. <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Fix getCookieCache to respect custom cookie names without a prefix. When cookiePrefix is an empty string, it now searches using only cookieName, preventing null cache results. - **Bug Fixes** - Skip applying the default prefix when cookiePrefix is "" and preserve __Secure cookie handling. - Added tests for custom cookie names without a prefix and for wrong-prefix lookups returning null. <sup>Written for commit aa144b3aba222ca9027bd24372884712eddc3d92. Summary will update automatically on new commits.</sup> <!-- End of auto-generated description by cubic. --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-03-13 12:52:49 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#6264