Automatic Token Inclusion with Cookies
You'll learn: How cookies automatically attach to requests and why that convenience creates CSRF vulnerabilities.
The Automatic Sending Behavior
When you store a JWT (or any authentication token) in a cookie, browsers automatically include that cookie with every request to the same domain. You don't need to write JavaScript code to attach it manually—the browser does this for you.
This is fundamentally different from storing tokens in localStorage or memory. With those approaches, you must explicitly add the token to the Authorization header in your fetch() calls. With cookies, the browser reads the cookie jar and automatically appends relevant cookies to the request headers.
The convenience analogy: It's like having a library card permanently clipped to your jacket. Every time you walk into the library (make a request), the card goes with you automatically—you don't need to remember to grab it from your wallet each time.
The CSRF Trade-Off
This automatic behavior creates the CSRF (Cross-Site Request Forgery) vulnerability you learned about earlier. Because any request to your domain includes the authentication cookie—even requests initiated by malicious sites—an attacker can trick a user's browser into making authenticated requests without the user's knowledge.
If you're on evil.com and it contains code that submits a form to yourbank.com, your browser will automatically include your yourbank.com authentication cookie. The bank's server sees a valid cookie and processes the request.
This is why cookie-based authentication typically requires CSRF tokens or SameSite cookie attributes as additional protection layers.
Key Takeaway: Cookies provide effortless token inclusion with every request, but this automatic behavior means extra CSRF defenses are essential—the convenience comes with security responsibilities you must address.