Cookie domain issue #425

Closed
opened 2026-03-13 07:45:20 -05:00 by GiteaMirror · 12 comments
Owner

Originally created by @qasimali09 on GitHub (Dec 18, 2024).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

  1. Create an auth in Node.js Express project and deploy it on a live server.
  2. Use the auth client in the Next.js local server and log in.
  3. Auth is not working in the Next.js serverside because the cookies are set on the API domain.

Current vs. Expected behavior

Currently, the cookie domain is set from the base URL.
Need an option to set cookie options or cookie domain option dynamically. I want to set the cookie domain from the origin because I want to use the API in the live domain and local domain both.

What version of Better Auth are you using?

1.0.15

Provide environment information

- OS: macOS Sequoia
- Browser: Chrome

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

Backend, Client

Auth config (if applicable)

import { betterAuth } from "better-auth"
export const auth = betterAuth({
  emailAndPassword: {  
    enabled: true
  },
});

Additional context

No response

Originally created by @qasimali09 on GitHub (Dec 18, 2024). ### Is this suited for github? - [X] Yes, this is suited for github ### To Reproduce 1) Create an auth in Node.js Express project and deploy it on a live server. 2) Use the auth client in the Next.js local server and log in. 3) Auth is not working in the Next.js serverside because the cookies are set on the API domain. ### Current vs. Expected behavior Currently, the cookie domain is set from the base URL. Need an option to set cookie options or cookie domain option dynamically. I want to set the cookie domain from the origin because I want to use the API in the live domain and local domain both. ### What version of Better Auth are you using? 1.0.15 ### Provide environment information ```bash - OS: macOS Sequoia - Browser: Chrome ``` ### Which area(s) are affected? (Select all that apply) Backend, Client ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth" export const auth = betterAuth({ emailAndPassword: { enabled: true }, }); ``` ### Additional context _No response_
GiteaMirror added the bug label 2026-03-13 07:45:20 -05:00
Author
Owner

@emroot commented on GitHub (Dec 18, 2024):

have you tried setting up this way this in the config?

advanced: {
    ...
    crossSubDomainCookies: {
      enabled: true,
      domain: 'yourdomain.com',
    },
  },

if you want to set dynamically, you could use your env

const MyDomains = {
  development: 'yourdomain.local',
  production: 'yourdomain.com',
  staging: 'yourdomain.staging',
};
...
advanced: {
    ...
    crossSubDomainCookies: {
      enabled: true,
      domain: MyDomains[process.node.NODE_ENV],
    },
  },


@emroot commented on GitHub (Dec 18, 2024): have you tried setting up this way this in the config? ``` typescript advanced: { ... crossSubDomainCookies: { enabled: true, domain: 'yourdomain.com', }, }, ``` if you want to set dynamically, you could use your env ``` typescript const MyDomains = { development: 'yourdomain.local', production: 'yourdomain.com', staging: 'yourdomain.staging', }; ... advanced: { ... crossSubDomainCookies: { enabled: true, domain: MyDomains[process.node.NODE_ENV], }, }, ```
Author
Owner

@daveycodez commented on GitHub (Dec 18, 2024):

@emroot will this only work with subdomains?

@daveycodez commented on GitHub (Dec 18, 2024): @emroot will this only work with subdomains?
Author
Owner

@emroot commented on GitHub (Dec 18, 2024):

I mean it will work on all subdomains for whatever you specify there, but I don't why it wouldn't work with a different domain, if you own it of course.
In my case I have configured my domains as is: mydomain.local for my local machine, and mydomain.com for my prod domain, and it's dynamic based on my env. I also set the trustedDomains dynamically too based on env.

@emroot commented on GitHub (Dec 18, 2024): I mean it will work on all subdomains for whatever you specify there, but I don't why it wouldn't work with a different domain, if you own it of course. In my case I have configured my domains as is: mydomain.local for my local machine, and mydomain.com for my prod domain, and it's dynamic based on my env. I also set the trustedDomains dynamically too based on env.
Author
Owner

@daveycodez commented on GitHub (Dec 20, 2024):

Are we only able to set it to one domain? If I want to use it with localhost I'm testing domain: "localhost" and domain ".app.localhost"

Or should I test with "127.0.01." for domain. I want to be able to hit my auth endpoints on production from my local static export and still get cookies

@daveycodez commented on GitHub (Dec 20, 2024): Are we only able to set it to one domain? If I want to use it with localhost I'm testing domain: "localhost" and domain ".app.localhost" Or should I test with "127.0.01." for domain. I want to be able to hit my auth endpoints on production from my local static export and still get cookies
Author
Owner

@daveycodez commented on GitHub (Dec 20, 2024):

This is how I got it to work:

{
    trustedOrigins: ["http://localhost:3000"],
    advanced: {
        defaultCookieAttributes: {
            sameSite: "none",
            secure: true
        }
    },
}
@daveycodez commented on GitHub (Dec 20, 2024): This is how I got it to work: ```ts { trustedOrigins: ["http://localhost:3000"], advanced: { defaultCookieAttributes: { sameSite: "none", secure: true } }, } ```
Author
Owner

@qasimali09 commented on GitHub (Dec 20, 2024):

I want to handle multiple origins. If I use the production API in localhost, it will not work because the cookie saved in the production domain and localhost cannot access the cookie server-side. It will work fine on the client side.

@qasimali09 commented on GitHub (Dec 20, 2024): I want to handle multiple origins. If I use the production API in localhost, it will not work because the cookie saved in the production domain and localhost cannot access the cookie server-side. It will work fine on the client side.
Author
Owner

@daveycodez commented on GitHub (Dec 20, 2024):

When you try to sign in, open the Network inspection tab in Chrome and look at the Response headers. You should see the cookie there and a yellow exclamation point, if you hover that exclamation point it will tell you why it won't set the cookie. What does it say for you?

@daveycodez commented on GitHub (Dec 20, 2024): When you try to sign in, open the Network inspection tab in Chrome and look at the Response headers. You should see the cookie there and a yellow exclamation point, if you hover that exclamation point it will tell you why it won't set the cookie. What does it say for you?
Author
Owner

@qasimali09 commented on GitHub (Dec 20, 2024):

When you try to sign in, open the Network inspection tab in Chrome and look at the Response headers. You should see the cookie there and a yellow exclamation point, if you hover that exclamation point it will tell you why it won't set the cookie. What does it say for you?

It’s because of a wrong cookie configuration.

@qasimali09 commented on GitHub (Dec 20, 2024): > When you try to sign in, open the Network inspection tab in Chrome and look at the Response headers. You should see the cookie there and a yellow exclamation point, if you hover that exclamation point it will tell you why it won't set the cookie. What does it say for you? It’s because of a wrong cookie configuration.
Author
Owner

@emroot commented on GitHub (Dec 20, 2024):

You could set up nginx locally to make that work. That's why I do on my end.
My prod domain is in this fornat my domain.com, and my local domain through nginx is local.mydomain.com.
By setting crossdomaincookies to domain.com it should just work.

@emroot commented on GitHub (Dec 20, 2024): You could set up nginx locally to make that work. That's why I do on my end. My prod domain is in this fornat my domain.com, and my local domain through nginx is local.mydomain.com. By setting crossdomaincookies to domain.com it should just work.
Author
Owner

@qasimali09 commented on GitHub (Dec 20, 2024):

You could set up nginx locally to make that work. That's why I do on my end. My prod domain is in this fornat my domain.com, and my local domain through nginx is local.mydomain.com. By setting crossdomaincookies to domain.com it should just work.

Thanks for the quick fix, but could you please add an option to set the cookie to all trusted origins?

@qasimali09 commented on GitHub (Dec 20, 2024): > You could set up nginx locally to make that work. That's why I do on my end. My prod domain is in this fornat my domain.com, and my local domain through nginx is local.mydomain.com. By setting crossdomaincookies to domain.com it should just work. Thanks for the quick fix, but could you please add an option to set the cookie to all trusted origins?
Author
Owner

@edmilsonrobson commented on GitHub (Feb 5, 2025):

This is how I got it to work:

{
trustedOrigins: ["http://localhost:3000"],
advanced: {
defaultCookieAttributes: {
sameSite: "none",
secure: true
}
},
}

Thanks, this fixed it for me.

@edmilsonrobson commented on GitHub (Feb 5, 2025): > This is how I got it to work: > > { > trustedOrigins: ["http://localhost:3000"], > advanced: { > defaultCookieAttributes: { > sameSite: "none", > secure: true > } > }, > } Thanks, this fixed it for me.
Author
Owner

@guptaashwanee commented on GitHub (Jun 9, 2025):

This is how I got it to work:

{
trustedOrigins: ["http://localhost:3000"],
advanced: {
defaultCookieAttributes: {
sameSite: "none",
secure: true
}
},
}

Hi @daveycodez thanks for your reply, I also had the same issue, where my client and server are running in different system (IPs) and not running in https (development) due to this it is giving me another issue, as the "It had the "Secure" attribute but was not received over a secure connection."

@guptaashwanee commented on GitHub (Jun 9, 2025): > This is how I got it to work: > > { > trustedOrigins: ["http://localhost:3000"], > advanced: { > defaultCookieAttributes: { > sameSite: "none", > secure: true > } > }, > } Hi @daveycodez thanks for your reply, I also had the same issue, where my client and server are running in different system (IPs) and not running in https (development) due to this it is giving me another issue, as the "It had the "Secure" attribute but was not received over a secure connection."
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#425