flawopen.com/Reference/JWT vs. HttpOnly Cookies

JWT in localStorage vs. HttpOnly cookies: which is safer?

Authentication Guide
Short answer

HttpOnly SameSite cookies are safer for web applications. Storing tokens in localStorage makes them vulnerable to total exfiltration via any XSS flaw on your domain. HttpOnly cookies cannot be read by JavaScript, eliminating XSS token theft.

The Security Trade-Offs

Storage Location XSS Threat CSRF Threat Revocation Speed
localStorage Token can be stolen by script Immune to CSRF Difficult without server state
HttpOnly Cookie Script cannot access token Protected by SameSite=Lax Instant via server-side session

Recommended Architecture