Syntax - Tasty Web Development Treats
Syntax - Tasty Web Development Treats

Hasty Treat - Authentication: LocalStorage vs Cookies vs Sessions vs Tokens

In this Hasty Treat, Scott and Wes talk about authentication — the difference between localStorage, cookies, session, tokens and more! LogRocket - Sponsor LogRocket lets you replay what users do on your site, helping you reproduce bugs and fix issues faster. It’s an exception tracker, a session repl

Featured Speakers

Wes Bos & Scott Tolinski - Full Stack JavaScript Web Developers Host

Topics Discussed

Episode Summary

Executive Summary: The episode explains authentication fundamentals by contrasting JWTs and sessions, and local storage versus cookies for sending auth state with requests. The hosts emphasize that these choices are not true “versus” pairs, but different layers of the same problem: how to track a user and how to identify them on each request. They cover tradeoffs in statefulness, server rendering, and security (XSS vs CSRF), and note that tools like Auth0 or Passport can reduce implementation complexity.

Main Topics: JWTs vs. Sessions: The hosts compare token-based auth and session-based auth, explaining how JWTs are stateless and sessions require server-side state. Local Storage vs. Cookies: They distinguish where auth credentials are stored and how they travel with requests, focusing on explicit token sending versus automatic cookie handling. Server Rendering and UX: Cookies enable authenticated server-side rendering on first load, while local storage can cause a logged-out flash before client-side auth resolves. Security Tradeoffs: XSS and CSRF: The episode outlines the primary security risks of each approach: tokens in local storage are vulnerable to XSS, while cookie/session flows need CSRF protections. Practical Tooling and Framework Support: They discuss how frameworks, middleware, Passport.js, Auth0, and similar services simplify auth implementation and reduce custom work.

Key Arguments: JWTs are stateless, so they do not require a shared server-side session store, which works well for distributed systems, serverless apps, and microservices. Sessions are stateful and easier to invalidate centrally, but they require maintaining session data on the backend and cleaning it up when expired. Cookies are passive: they are automatically sent with requests, which makes authenticated server-side rendering possible without extra client-side code. Local storage requires explicit JavaScript to attach tokens to requests, which makes it less convenient for SSR and first-load auth state. JWTs are harder to revoke individually; logout often requires blacklisting tokens or changing signing logic, which can invalidate all users. Token-in-local-storage approaches are exposed to XSS because malicious JavaScript on the page can read the token from client-side storage. Cookie/session approaches are exposed to CSRF because browsers automatically attach cookies, allowing unintended cross-site requests unless protected. Auth services and libraries can reduce complexity, but they vary in scope: Passport helps implement auth mechanics, while Auth0 can handle more of the full auth flow and cross-domain concerns.

Data Points: Free trial length: 14 days - LogRocket sponsorship offer for listeners Auth architecture categories discussed: 2 - The episode frames the explanation around two questions: how to track users and how to identify them on each request Primary security risks compared: 2 - XSS is associated with tokens in client storage; CSRF is associated with cookie/session flows

Pivotal Quotes: "authentication versus cookies versus sessions. And I like this because it's a kind of a broken question where these things aren't necessarily versus." — Wes: Explaining that the episode is about related auth layers rather than direct alternatives "Token-based authentication is stateless, meaning that you don't need to maintain a list of logged in users on the back-end server." — Wes: Describing the main architectural advantage of JWT-based auth "the server-rendered auth on both of them." — Wes: Explaining why both of his courses use cookies with JWTs rather than local storage

Implications: For builders, the main takeaway is to choose auth storage and transport based on architecture, UX, and threat model—not habit. Stateless tokens suit distributed apps; sessions suit traditional server apps; cookies improve SSR; and security must be designed in, not bolted on.

🔓 Sign Up for Unlimited Episode Search

About Syntax - Tasty Web Development Treats

View all episodes from Syntax - Tasty Web Development Treats