C is for cookies
Adventures in creating a cross-domain cookie storage solution
About me
- Name: Mike Frazier
- Occupation: Software Engineer at Evernow
- Past life: High school science teacher
- Hobbies: (Skate/Snow)boarding, reading, music stuff
- Zodiac: Aries
- Twitter: @AFrazGuy
- DenverDevs: @frazier
- Recently reskinned blog: www.mikedoescoding.com
How it started
Hey Mike,
We are going to be splitting our marketing site from our members site. One will be hosted on a subdomain of ours. We will want a cookie to be available between the domains.
You good with that, dude?
Thanks!
The Boss
What happened Next
What really happened
Lots and lots of Stackoverflow search
- How to create cross-domain cookie
- How to share cookie between domain and sub-domain
- Domain set cookie for sub-domain
What is a cookie
According to Wikipedia, cookies "are small blocks of data created by a web server while a user is browsing a website and placed on the user's computer or other device by the user’s web browser. Cookies are placed on the device used to access a website, and more than one cookie may be placed on a user’s device during a session."
Cookies are useful for storing stateful types of information such as carts, authentication status, and user preferences.
What are the Specs for this?
Cookies have the following structure:
Name
Value
Optional Attributes
- Secure (https?)
- Expiry date
- Domain
- Others
Let's Jump to an example
Set up and Limitations
Dumb little Vite React application
Using a cookie library to help set the cookies
How do we get this to work in dev? Localhost NEVER acts the same as a production server
What I learned
Setting the domain key on the cookie will either create a host-only (accessible only on the domain that set it) or one available on subdomains
It's possible to make localhost act like a real server by updating the /etc/hosts file
Hopefully by having done this once, I won't forget or end up in Stackoverflow hell again
Thanks
C is for Cookies
By Mike Frazier
C is for Cookies
- 48