Track 07 · Lesson 2 of 18Identity and login
- 01Authentication vs authorization
- 02Browser storage compared
- 03Sessions versus tokens
- 04Inside a JWT
- 05Access and refresh tokens
- 06Where to keep tokens
- 07Choosing a design
- 08The login lifecycle
- 09The token clock
- 10Dead or just unlucky? Refresh decisions
- 11Inside a secure session cookie
- 12Five tabs, one refresh
- 13“Log in with Google”: OAuth and PKCE
- 14Two-factor login
- 15Storing passwords
- 16Logging out properly
- 17Trade-offs and common mistakes
- 18Compare the designs
Browser storage compared
Lesson 2 of 18· 7 min
You can keep data in a JavaScript variable, sessionStorage, localStorage, IndexedDB or a cookie. They differ in how long data lasts, which tabs see it, who can read it, and whether it travels to the server. These differences decide where a login can safely live.
Five places, six events
Save a value, then make things happen
Then make something happen
| Row | JS variablelet cart = 3 | sessionStoragesessionStorage.setItem("cart", "3") | localStoragelocalStorage.setItem("cart", "3") | IndexedDBstore.put({ id: "cart", items: 3 }) | Cookie (HttpOnly)Set-Cookie: cart=3; Max-Age=2592000; HttpOnly |
|---|---|---|---|---|---|
| Right now | Empty Nothing saved yet. | Empty Nothing saved yet. | Empty Nothing saved yet. | Empty Nothing saved yet. | Empty Nothing saved yet. |
| Space | Memory you have | About 5 MB | About 5 MB | Large: a share of free disk | About 4 KB per cookie |
| Lasts until | Reload | The tab closes | Cleared | Cleared | Its expiry, or the browser closes |
| Shared with | This page only | This tab only | All tabs, same origin | All tabs, same origin | All tabs, its domain |
| Sent to the server | No | No | No | No | Yes, automatically |
| How you use it | Variables | Sync, strings only | Sync, strings only | Async, objects and files | Set-Cookie or document.cookie |
Try the real APIs on this page
// Pick an action
Reload and read again: both survive, because a reload keeps the same tab. Close the tab and come back, and only localStorage remains. This site keeps your lesson progress in localStorage too.
How long it lasts#
A reload wipes memory, and only memory. sessionStorage survives reloads but belongs to one tab. localStorage and IndexedDB stay until cleared, though Safari may delete them after seven days without a visit. Cookies last until their Max-Age or Expires, or until the browser closes if they have neither.
Who can read it#
Every kind of storage JavaScript can read, an injected script can read too. That’s the line that matters for auth: an HttpOnly cookie is the only option a script can’t read at all. Other websites can never read your storage, thanks to the same-origin policy, but your cookies can still ride along on their requests to you, depending on SameSite.
What travels to the server#
Only cookies are sent automatically, with every request to their domain, even images. That’s convenient for sessions, and it’s exactly what cross-site request forgery exploits. Everything else is sent only when your code adds it.
Check yourself