Stop Passing Tokens in URLs: The Right Way to Share Sessions Across Subdomains
How I learned to ditch the “URL token relay” and embrace the power of HTTP Cookies.
When I first started as a Junior Developer, I inherited a legacy project that consisted of multiple platforms under one main company domain. We had the main landing page (example.com), a dashboard (app.example.com), and a store (shop.example.com).
From a user’s perspective, it was all one ecosystem. They expected that once they logged in on the main page, they would stay logged in when they clicked “Go to Dashboard.”
But technically, these were separate applications. And keeping the user logged in across them was a nightmare.
The “Junior” Fix: The URL Relay Race
At that time, I didn’t know much about authentication best practices. I looked at the existing legacy code to see how they handled it.
The logic seemed simple — but hacky:
- User logs in on example.com. We save the JWT token in LocalStorage.
- User clicks a link to app.example.com.
- Before redirecting, we grab the token from LocalStorage and append it to the URL query parameter. window.location.href = ‘https://app.example.com?token=’ + userToken;
- The destination app checks the URL, grabs the token, and saves it into its own LocalStorage.
At first glance, this “URL Relay” method made sense to my junior brain. It worked… mostly.
The Problem: “Why am I logged out?”
The cracks started to show when I was tasked with building a feature that required frequent jumping between platforms.
One day, a bug report came in: “I was logged in, but when I clicked the link to the store, it asked me to login again.”
I investigated and realized the issue: Human Error. I had created a simple HTML link () but forgot to attach the special click handler that appends the token to the URL. Because the token wasn’t passed, the second app didn’t know who the user was.
Besides being fragile, I started realizing other issues:
- Security Nightmare: The authentication token was visible in the browser URL bar. If a user shared that link or someone checked their browser history, their session could be hijacked.
- Messy Code: Every single link between apps needed custom logic.
I thought, “There has to be a standard way to do this. Facebook and Google don’t put tokens in their URLs.”
The Solution: Understanding Cookie Scope
I spent my spare time researching “Cross-subdomain authentication.” That’s when I learned about the Domain Attribute in HTTP Cookies.
By default, cookies are “host-only.” If you set a cookie on www.example.com, it is only visible to that specific subdomain. app.example.com cannot see it.
However, if you explicitly set the domain attribute with a leading dot (or just the root domain in modern browsers), the cookie becomes available to the main domain and all its subdomains.
The fix was incredibly simple. Instead of LocalStorage, we set the session cookie like this:
// The magic lies in the 'domain' attribute
document.cookie = "session_token=xyz123; path=/; domain=.example.com; Secure; SameSite=Lax";
(Note: In a real production app, this should ideally be Set-Cookie header from the server with HttpOnly flag for better security, but the concept of the domain attribute remains the same).
The Result: Seamless Magic
I refactored our authentication logic during a sprint gap.
The “After” workflow:
- User logs in at example.com.
- We set a cookie with domain: .example.com.
- User clicks a normal link to app.example.com.
- The Browser automatically sends the cookie to the subdomain.
- The app reads the cookie, validates it, and the user is logged in instantly.
No more tokens in URLs. No more custom click handlers. No more “Why did I get logged out?” complaints.
The Takeaway
As a junior developer, it’s easy to invent creative workarounds (like passing tokens in URLs) because “it just works.” But usually, the “boring” standard way — understanding how HTTP state management actually works — is the best solution.
If you are building a multi-subdomain architecture, stop using LocalStorage and URL params. Embrace the power of shared cookies. It’s cleaner, safer, and keeps your users happy.