React Course
React
/
Advanced

Token Storage

Definition

The secure management of authentication tokens (like JWTs) in a React application to prevent theft via XSS or CSRF attacks.

Explain Like I'm New

If you leave your house key under the doormat (localStorage), a thief can easily find it and break in (XSS). If you give your key to the bank and they check your ID every time (HttpOnly Cookies), it's much safer.

Real World Example

Instead of storing a `jwt` in `localStorage.setItem('token', jwt)`, you configure your backend to send the token as an `HttpOnly` cookie. This makes it impossible for malicious JavaScript to read the token.

Common Use Cases

  • Securing user sessions
  • Preventing XSS data theft

Interactive Example

Loading...
Console output will appear here...

Interview Questions

basic

  • Is it safe to store JWTs in localStorage?

intermediate

  • What is an HttpOnly cookie?

advanced

  • How do you handle CSRF attacks if you use Cookies?

Flash Cards

Question

Is it safe in localStorage?

Click to reveal answer
Answer

Generally, no. Any JavaScript running on your page (including third-party analytics or malicious XSS scripts) can read localStorage and steal the token.