Local Storage vs Session Storage vs Cookies: Where to Store an Auth Token?

Compare browser storage options, understand cookie behavior, and learn why secure HttpOnly cookies are commonly used for authentication.

Local Storage vs Session Storage vs Cookies: Where to Store an Auth Token?

Local Storage vs. Session Storage vs. Cookies

Browsers offer several ways to keep data between page loads. The three options beginners often compare are localStorage, sessionStorage, and cookies. Their lifetimes, access methods, and relationship to HTTP requests are different.

StorageLifetime and scopeSent automatically with HTTP requests?Common uses
localStoragePersists for the origin until cleared; shared by same-origin pagesNoUI preferences, non-sensitive client data
sessionStorageScoped to an origin and a browser tab; usually cleared when that tab's page session endsNoTemporary, tab-specific form or navigation state
CookieLifetime and scope set by cookie attributes; may be session-onlyYes, when domain, path, and security rules matchServer-managed sessions, request-related state

Web Storage holds string key/value pairs and is accessed through JavaScript. A cookie is stored by the browser and included in matching requests. This automatic behavior is why cookies are useful for sessions, and why cookie size and security attributes matter. MDN: Web Storage API · MDN: Using HTTP cookies

Local Storage

localStorage persists across browser restarts until the user or application clears it. It is available to JavaScript running on the same origin, and its values are strings:

localStorage.setItem("theme", "dark");

const theme = localStorage.getItem("theme");
console.log(theme); // "dark"

localStorage.removeItem("theme");

For objects, convert to and from JSON:

const preferences = { theme: "dark", compact: true };
localStorage.setItem("preferences", JSON.stringify(preferences));

const saved = JSON.parse(localStorage.getItem("preferences"));

Use it for data such as a theme or dismissed tip that should persist. Avoid storing session identifiers, refresh tokens, or other credentials in Web Storage: any JavaScript executing in the origin can read them. A cross-site scripting (XSS) flaw can therefore expose those values. OWASP: HTML5 Security

Session Storage

sessionStorage has a similar API, but it is scoped to the current origin and tab. Its data is generally removed when that tab's page session ends:

sessionStorage.setItem("checkoutStep", "shipping");
const step = sessionStorage.getItem("checkoutStep");

It can suit temporary tab-specific values, such as a multi-step form's current step. It is still accessible to JavaScript in the page, so it is not a safer place for an authentication credential. MDN: Web Storage API

Cookies

Cookies can be set by a server using the Set-Cookie response header. The browser includes matching cookies in later HTTP requests. Cookies can have expiration, domain, path, and security attributes; they are not general-purpose large data storage. They are commonly used to carry a session identifier that the server uses to look up the user's session.

Unlike localStorage and sessionStorage, cookies are sent automatically. That helps with server-managed sessions but creates a cross-site request forgery (CSRF) consideration: a browser may attach a cookie to a request initiated from another site, depending on cookie settings and request context. Cookie-authenticated applications need appropriate CSRF defenses. MDN: Using HTTP cookies · OWASP: CSRF Prevention

Where would you store an auth token?

For many first-party browser applications, a common strong default is to let the server manage the session and send the browser a session cookie configured with HttpOnly, Secure, and an appropriate SameSite value. This avoids exposing the cookie value to application JavaScript and reduces the chance that an XSS bug can directly steal it. OWASP recommends against storing session identifiers or authentication tokens in localStorage or sessionStorage. OWASP: Session Management

The server can create a cookie in an HTTP response, for example:

Set-Cookie: __Host-session=opaque-session-id; Path=/; Secure; HttpOnly; SameSite=Lax

This is an illustrative header. In production, use your framework's session facilities and choose cookie scope and lifetime for your application. A server-side session identifier is often preferable to putting a long-lived access token in the browser cookie.

An HttpOnly cookie cannot be read by page JavaScript, including with document.cookie. That helps prevent direct token theft through XSS, but it does not stop malicious script running in the page from making requests that use the user's existing session. Continue to prevent XSS and enforce authorization on the server.

Because cookies are sent automatically, also use CSRF defenses appropriate to your architecture. SameSite is useful defense-in-depth, but it is not a universal replacement for CSRF protection. A cross-site cookie typically requires SameSite=None; Secure; many same-site applications can use Lax or Strict, depending on login and navigation needs. MDN: Session management · OWASP: Session Management

Authentication architecture varies. A backend-for-frontend (BFF), server-rendered app, and browser-only OAuth client may use different token flows. Follow the framework and identity provider's current guidance rather than choosing storage in isolation.

Quick decision guide

  • Save a theme or a non-sensitive preference across visits: localStorage.
  • Keep temporary state specific to one tab: sessionStorage.
  • Maintain a server-managed browser session: usually a server-set cookie with carefully chosen attributes and CSRF defenses.
  • Store a bearer credential in Web Storage: generally avoid it, because page JavaScript can read it.

Try it yourself

Use the browser console to save a theme in localStorage, then close and reopen the page to see that it remains. Save a temporary step in sessionStorage, open the same site in another tab, and compare the values. In your notes, explain why neither storage API is appropriate for a session token.

Recap

  • Local Storage persists across sessions; Session Storage belongs to a tab's page session.
  • Both Web Storage APIs store strings and are readable by same-origin JavaScript.
  • Cookies can be attached automatically to matching HTTP requests and support server-managed sessions.
  • Prefer a carefully configured server session cookie over putting credentials in Web Storage for many first-party apps.
  • HttpOnly reduces direct token theft by JavaScript, but does not neutralize XSS; cookie auth also needs CSRF defenses.

What's next?

Next, we’ll explain what HttpOnly, Secure, and SameSite mean on a cookie.

Written by

Dev Likhon

Dev Likhon

Full Stack Developer & Tech Educator

6+ years of professional development experience.

About Me
Share:

Comments

Share what you learned or ask a question.

Loading comments...