{
  "id": 17066151,
  "title": "Stop Storing JWTs in localStorage. Here's Why.",
  "url": "https://urgent.news/2026/10/29/stop-storing-jwts-in-localstorage-heres-why",
  "topic": "tech",
  "section": "Tech",
  "published": "2026-10-29T13:30:00.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/ihssmaheel/stop-storing-jwts-in-localstorage-heres-why-3l7e"
  },
  "original_language": "en",
  "account": "Many tutorials suggest storing JWTs in localStorage when implementing authentication. It seems straightforward: save the authentication token and include it with each API request. However, this approach has a significant flaw. Any script running on your webpage can access this token, including malicious JavaScript. If your site has an XSS vulnerability, an attacker could steal the token and gain access to the user's account until it expires or is revoked. This risk isn't specific to JWTs; any sensitive data stored in localStorage is vulnerable. A better solution is to use HttpOnly cookies instead of localStorage. Set-Cookie headers can help secure these cookies by limiting access to JavaScript. HttpOnly means JavaScript can't read the cookie, Secure ensures the browser only sends it over HTTPS, and SameSite=Strict prevents cross-site requests from sending the cookie. This way, even if an attacker exploits XSS, they can't access the HttpOnly cookies. However, HttpOnly cookies aren't foolproof. Malicious JavaScript can still send authenticated requests, and CSRF attacks can still occur. To mitigate CSRF risks, implement proper protections like validating request origins, using SameSite cookies, and adding CSRF tokens where necessary. The double-submit cookie pattern, where the server validates a CSRF token with each request, is a common approach. Additionally, using short-lived JWTs and rotating signing keys securely is crucial. By issuing tokens with limited lifetimes and carefully managing key rotation, you can minimize the impact of potential compromises. While localStorage can still be used for non-sensitive settings, JWTs for authentication should be stored securely using HttpOnly cookies. Authentication is about more than just login; it requires careful consideration of how tokens are stored, protected, and revoked when needed.",
  "summary": "You've probably seen this in a React authentication tutorial: localStorage . setItem ( \" token \" , response . token ); User logs in. Save the JWT. Send it with every API request. Simple, right? There's one problem: any JavaScript running on your page can read that token. And that includes malicious JavaScript. What's Actually Wrong With localStorage? Imagine your application has an XSS…",
  "key_points": [],
  "editors_take": null,
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "ai_generated": true,
  "disclaimer": "Summaries, key points and the editor’s take are written by software from other outlets’ reporting and may contain errors — always check the linked original."
}