I’m working on a NextJS project where I have multiple apps on separate subdomains. My goal is to login in one app and automatically signed in at every other. We’re using nookies as cookie handler, creating a cookie with a JWT token payload received from an API. I thought if I set the cookie domain manually then it’s going to set the cookie on the main domain but it did not happen.
Here’s what I’ve tried:
setCookie(
null,
"token",
`JWT ${data.tokenAuth.token}`,
{
maxAge: 29 * 24 * 60 * 60,
path: "/",
domain: process.env.NEXT_PUBLIC_COOKIE_DOMAIN,
}
);
I’ve set the NEXT_PUBLIC_COOKIE_DOMAIN
as "example.com"
and ".example.com"
, neither of them worked, my cookie was always set to the current subdomain. I also got the idea to put the login page under “example.com/login” to set the cookie on the main domain so I can access it everywhere, but I wonder if there’s a solution to avoid it. I’ve already read about RFC 6265, from which I assume it’s only possible from the main domain, yet the tracking took we’re using somehow can assign “.example.com” for it’s cookie. What am I missing? Thanks for replies in advance.
Entersprite is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.