Bird
0
0

How can you securely implement a token refresh mechanism in a single-page application (SPA) to avoid exposing refresh tokens to JavaScript?

hard📝 Application Q9 of 15
Rest API - Authentication and Authorization
How can you securely implement a token refresh mechanism in a single-page application (SPA) to avoid exposing refresh tokens to JavaScript?
ASend refresh tokens as URL parameters in AJAX calls
BStore refresh tokens in localStorage for easy access
CStore refresh tokens in HttpOnly cookies and use silent refresh requests
DEmbed refresh tokens in HTML meta tags
Step-by-Step Solution
Solution:
  1. Step 1: Identify secure storage for refresh tokens

    HttpOnly cookies prevent JavaScript access, reducing XSS risk.
  2. Step 2: Use silent refresh requests

    Silent refresh via cookies allows token renewal without exposing tokens to scripts.
  3. Final Answer:

    Store refresh tokens in HttpOnly cookies and use silent refresh requests -> Option C
  4. Quick Check:

    HttpOnly cookies protect refresh tokens in SPAs [OK]
Quick Trick: Use HttpOnly cookies to protect refresh tokens [OK]
Common Mistakes:
  • Storing tokens in localStorage (XSS risk)
  • Exposing tokens in HTML or URLs
  • Not protecting tokens from JavaScript

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Rest API Quizzes