[GH-ISSUE #2969] defaultCookieAttributes are not used when using createAuthCookie() #9419

Closed
opened 2026-04-13 04:52:54 -05:00 by GiteaMirror · 2 comments
Owner

Originally created by @gregg-cbs on GitHub (Jun 10, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/2969

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

  1. I am using ctx.context.createAuthCookie("session_token")
  2. It returns cookie attributes but those attributes are not the defaultCookieAttributes i set in my betterAuth config,

Current vs. Expected behavior

I expect defaultCookieAttributes to be used when doing anything with cookies in betterAuth

What version of Better Auth are you using?

1.2.8

Provide environment information

Windows
Nodejs

Which area(s) are affected? (Select all that apply)

Backend

Auth config (if applicable)

export const guestSessionPlugin = (): BetterAuthPlugin => ({
  id: "guest-session-plugin",
  endpoints: {
    createGuestSession: createAuthEndpoint(
      "/guest-session/create",
      { method: "GET" },
      async (ctx) => {
        const c = ctx.context.createAuthCookie("session_token")
        c.attributes // c.secure is true
      }
    )
  }
})

auth = betterAuth({
  advanced: {
    defaultCookieAttributes: {
      secure: false, // as an example this is false (needed for safari local dev testing)
      httpOnly: true,
      sameSite: "none",
      partitioned: true
    }
  }
})

Additional context

No response

Originally created by @gregg-cbs on GitHub (Jun 10, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/2969 ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce 1. I am using `ctx.context.createAuthCookie("session_token")` 2. It returns cookie attributes but those attributes are not the defaultCookieAttributes i set in my betterAuth config, ### Current vs. Expected behavior I expect defaultCookieAttributes to be used when doing anything with cookies in betterAuth ### What version of Better Auth are you using? 1.2.8 ### Provide environment information ```bash Windows Nodejs ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript export const guestSessionPlugin = (): BetterAuthPlugin => ({ id: "guest-session-plugin", endpoints: { createGuestSession: createAuthEndpoint( "/guest-session/create", { method: "GET" }, async (ctx) => { const c = ctx.context.createAuthCookie("session_token") c.attributes // c.secure is true } ) } }) auth = betterAuth({ advanced: { defaultCookieAttributes: { secure: false, // as an example this is false (needed for safari local dev testing) httpOnly: true, sameSite: "none", partitioned: true } } }) ``` ### Additional context _No response_
GiteaMirror added the locked label 2026-04-13 04:52:54 -05:00
Author
Owner

@gregg-cbs commented on GitHub (Jun 10, 2025):

Even worse when u use the context to set the cookie it always changes secure to true:

        // create session cookie config
        const c = ctx.context.createAuthCookie("session_token")

        const cookie = {
          name: c.name, 
          token: mapped_session.token, 
          options: ctx.context.options.advanced?.defaultCookieAttributes
        }

       // cookie.options.secure is false (as expected)
        
        // sets the cookie using the above session cookie config
        ctx.setCookie(cookie.name, cookie.token, cookie.options);

       // now cookie.options.secure is true (not expected for ctx.setCookie to override my cookie options and change them)
<!-- gh-comment-id:2958693448 --> @gregg-cbs commented on GitHub (Jun 10, 2025): Even worse when u use the context to set the cookie it always changes secure to true: ```ts // create session cookie config const c = ctx.context.createAuthCookie("session_token") const cookie = { name: c.name, token: mapped_session.token, options: ctx.context.options.advanced?.defaultCookieAttributes } // cookie.options.secure is false (as expected) // sets the cookie using the above session cookie config ctx.setCookie(cookie.name, cookie.token, cookie.options); // now cookie.options.secure is true (not expected for ctx.setCookie to override my cookie options and change them) ```
Author
Owner

@gregg-cbs commented on GitHub (Jun 10, 2025):

It looks like betterAuth is fine. This is a cookie issue where if partitioned is true and samesite is none then secure is forced to true as part of cookie policies.

Setting partitioned to false and samesite to lax in development solved this issue for me.

<!-- gh-comment-id:2958738042 --> @gregg-cbs commented on GitHub (Jun 10, 2025): It looks like betterAuth is fine. This is a cookie issue where if partitioned is true and samesite is none then secure is forced to true as part of cookie policies. Setting partitioned to false and samesite to lax in development solved this issue for me.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#9419