DEV Community

Cover image for Who Moved My Cookies? Of Cookies On Subdomains

Who Moved My Cookies? Of Cookies On Subdomains

Jen Chan on July 09, 2025

Yes, it's 2025, we had the internet and the encyclopedic knowledge of LLMs at our fingertips, and we were still blocked for 45 minutes. "Maze ga...
Collapse
 
shakti_kathpalia profile image
Shakti Kathpalia

We faced a similar issue for a project we're developing. Our laravel backend (a subdomain) generated the cookie that had to be validated in Next.js frontend (another subdomain) and another 3rd party system (yet another subdomain) which only worked with cookies. The Next frontend also authenticated the laravel session on every refresh through an API before fetching user-specific data.

The only solution was to create a cookie in the backend with the domain set to the root domain and voila, everything worked seamlessly.

Collapse
 
jenc profile image
Jen Chan

We were in a very similar situation except proxied by Cloudflare and using Nuxt. I think going through Cloudflare workers added a bit of complexity and cross checking overhead.

Collapse
 
shiva_shanker_k profile image
shiva shanker

Haha, this is so relatable! 😂 Cookie authentication issues are the worst - works perfectly in dev but production mein everything breaks

Collapse
 
jenc profile image
Jen Chan

The struggle with systems you can't see or debug every layer of results in... testing on prod! XD

Collapse
 
mezieb profile image
Okoro chimezie bright

Thanks for sharing helpful tips

Collapse
 
dotallio profile image
Dotallio

Yes, hitting that wall with cookie domains is always so frustrating. Did you end up considering tokens in URL params or localStorage at all for those mirror domains?

Collapse
 
jenc profile image
Jen Chan

I didn't think to put the tokens in the URL params, and I guess the way we were doing it wasn't entirely standard unlike OAuth2 url params including tokens.

The team abused LocalStorage before for less sensitive values so our Veep discouraged that as a practice... which left us with that cookie 😆

Collapse
 
nathan_tarbert profile image
Nathan Tarbert

Man, you summed up months of cookie pain in one post. I've been tripped up by that 'Domain' attribute headache so many times, it's honestly nice seeing it all laid out like this

Collapse
 
jenc profile image
Jen Chan

No kidding, working on that system with 3 layers of backend (MySQL, laravel, cloudflare edge proxy), so many mirror domains and no real way to run it all locally was crazy-making!

Glad I'm not the only one who's baffled and stumped!

Collapse
 
donahere profile image
donahere

Awesome easy-to-follow and meaningful post @jenc 👍👍👍

Collapse
 
jenc profile image
Jen Chan

Thanks! So many things on the job are still hairy mysteries even with LLMs helping to debug!