Web SecurityJuly 18, 20268 min read

Bulletproof Authentication Architecture: JWT Access Tokens with Rotating HttpOnly Refresh Cookies

Nguyen Dai Long

Nguyen Dai Long

Backend Lead & Software Engineer

JSON Web Tokens (JWT) are ubiquitous for stateless authorization across modern microservices and web clients. Yet countless web applications commit grave security vulnerabilities by storing raw JWT access tokens in browser localStorage—where any third-party script or XSS vulnerability can extract them. Here is how to architect a secure authentication pipeline.

1. The Vulnerability of localStorage and the HttpOnly Cookie Solution

Browser localStorage is accessible to any JavaScript running on the page. Storing JWTs there means a single compromised npm dependency or XSS vulnerability leaks the session token. By contrast, HttpOnly cookies cannot be accessed via document.cookie, completely neutralizing XSS token exfiltration.

Key Implementation Takeaways:

  • Never store authentication tokens in localStorage or sessionStorage.
  • Keep JWT access tokens short-lived (5 to 15 minutes) in client memory.
  • Store refresh tokens in secure, HttpOnly, SameSite=Lax cookies.

2. Automatic Refresh Token Rotation & Breach Detection

Every time a refresh token is used to obtain a new access token, the auth server invalidates the old refresh token and issues a new one. If an invalidated refresh token is ever presented again, it indicates a compromised session—prompting the server to immediately revoke the entire token family.

Key Implementation Takeaways:

  • Rotate refresh tokens on every single usage.
  • Implement token reuse detection to automatically terminate compromised sessions.
  • Deploy Redis token revocation lists (TRL) for instantaneous user sign-out.

Summary & Final Thoughts

Combining memory-held access tokens with rotating HttpOnly refresh cookies provides peak defense against both XSS token theft and CSRF attacks.

Engineering Feedback0 likes

Was this technical breakdown helpful for your production workflow?

Nguyen Dai Long

Written by Nguyen Dai Long

Backend Engineer & Backend Lead with 4+ years of hands-on experience building production systems, RESTful APIs, and cloud infrastructure using Python (Django), Laravel, PostgreSQL, and Google Cloud Platform.

Technical Discussion0

Ask questions, challenge architectures, or share your own production insights.

Join the Technical Community Discussion

Sign in via GitHub or Google in 5 seconds to comment, exchange architecture insights, and build your engineering presence.

Loading discussion...
NDL Ecosystem

Explore Free Web Tools & Games

View All Tools & Apps