Dev.to
5/14/2026

Stop Storing JWTs in localStorage: A Security Guide for Web Developers
Short summary
Storing JWTs in localStorage exposes tokens to XSS attacks where malicious scripts steal credentials and gain full account access. HttpOnly cookies solve this by making tokens inaccessible to JavaScript while the browser handles them automatically, protected by secure and sameSite flags. Reserve localStorage for non-sensitive data only; switching to HttpOnly cookies is one of the simplest security improvements web developers can make.
- •localStorage JWTs are vulnerable to XSS attacks via JavaScript access
- •HttpOnly cookies are automatically sent by browser and immune to script access
- •Use localStorage only for non-sensitive data like UI preferences
Generated with AI, which can make mistakes.
Is this a good recommendation for you?



