Don't store JWTs in localStorage: secure authentication architecture with Express and Redis

Protect your apps: don't store JWTs in localStorage. Implement dual token with Express, HttpOnly cookies, and Redis for instant revocation. Mitigate XSS and CSRF.

miércoles, 8 de julio de 2026 • 2 min read • Q2BSTUDIO Team

How to implement revocable sessions with dual token

In the current ecosystem of backend development with Node.js and Express, authentication using JWT tokens has become a standard. However, storing those tokens in the browser's localStorage is a practice that exposes the application to serious Cross-Site Scripting (XSS) vulnerabilities. At Q2BSTUDIO, we understand that cybersecurity is a fundamental pillar in any project, and therefore we recommend a dual-token architecture that combines storage in RAM, HttpOnly cookies with restricted scope, and Redis for session revocation. This approach, similar to what we implement in our custom application developments, elevates security to an enterprise level without sacrificing user experience.

The system is based on two tokens: the Access Token, with a short lifespan of 15 minutes, stored exclusively in a frontend RAM variable. Thus, if an attacker manages to execute malicious JavaScript, they will not find the token in persistent storage. The Refresh Token, valid for 7 days, is sent to the browser as an HttpOnly cookie, inaccessible to JavaScript, and with a path strictly limited to the renewal endpoint. This restriction prevents the cookie from being sent with every request, also mitigating CSRF attacks through the sameSite: 'lax' directive. Configuring these parameters is critical and is part of the best practices we apply in our cybersecurity projects.

One of the problems with stateless JWTs is the inability to revoke them instantly. To solve this, we integrate Redis as a high-speed session manager. Upon login, we store the Refresh Token in Redis associated with the user ID, allowing single-device session enforcement. When the user attempts to renew their access token, the backend verifies that the token stored in Redis matches the one presented; otherwise, the request is denied. Upon logout, we delete the entry in Redis and clear the cookie, ensuring immediate revocation even if the token was compromised. This pattern is part of our AWS and Azure cloud service solutions, where consistency and security are key.

The described architecture requires more infrastructure than a simple setItem, but the benefit in terms of protection against XSS and CSRF is enormous. At Q2BSTUDIO, we apply this type of design in our custom software developments, combining them with artificial intelligence technologies and AI agents to automate authentication and monitoring processes. Additionally, we integrate Power BI dashboards to visualize security metrics and active sessions, and we use business intelligence services to analyze access patterns. Our team also implements AI for businesses that enhance real-time anomaly detection. All of this is part of a global cybersecurity strategy that protects both the frontend and backend.

If you are developing an application with Express and looking for robust authentication, do not hesitate to contact Q2BSTUDIO. Our experience in custom applications and cloud service integration allows us to offer tailored solutions that meet the highest security standards. Remember: storing JWTs in localStorage is an avoidable risk; a dual-token architecture with Redis is the professional answer.

A BREAK?

Play for a moment before you go

OUR SERVICES

How we can help you

Do you have a project in mind?

Tell us your vision and we'll turn it into a software solution. Whatever the scope, we make your idea real.