Software Development Community Question

What are the main vulnerabilities when using JWT authentication in single page apps?

Posted on 2025-01-17 • In Software Development • By Cheryl Knight

Our web app uses React and a Node backend. We want to implement JWT authentication but are torn between using local storage or cookies due to conflicting online advice. Which path reduces XSS and CSRF risks?

3 Answers 17,576 Views

All answers to this question 3 Answers

Heather Bishop Answered on 2025-02-02
Answered

The most secure method for handling JWT authentication in single page applications is storing tokens in HttpOnly, Secure cookies with the SameSite attribute set to Strict or Lax. Storing tokens in local storage exposes them directly to cross-site scripting attacks, as any compromised third-party script can read the storage. While cookies do introduce cross-site request forgery vulnerabilities, this can be easily mitigated by validating custom anti-CSRF headers or relying on modern SameSite cookie protections supported by all major browsers.

Dennis Fowler Answered on 2025-02-20
Answered

If we switch completely to HttpOnly cookies, how can our frontend application access the custom user claims embedded inside the payload? Doesn't that force us to make an extra API call just to display the logged-in user's username?

Philip Dunn Commented on 2025-03-19
Commented

Dennis, a standard workaround is to split your token data or send an unencrypted secondary cookie containing just the user profile info for frontend UI rendering. The actual cryptographically signed token stays locked inside the HttpOnly cookie for backend API authorizations.

Gary Newton Answered on 2025-05-05
Answered

We avoid cookies entirely by keeping the token strictly in frontend memory and using a silent refresh token exchange hidden inside an invisible iframe.

Cheryl Knight Commented on 2025-05-12
Commented

Gary's approach is excellent for absolute isolation from storage, but it requires solid handling of browser tab sync so users don't lose session state when opening links in new windows.

Write a Comment

Your email address will not be published. Required fields are marked (*)

Join the LearnersEra Community

Ask questions, share knowledge and learn from professionals across technology, project management and certification topics.

Change Country and Nearest City

Select your country and nearest city to see courses available in your region.