Authentication Patterns for Frontend Apps
Sessions vs. tokens, how JWTs and OAuth fit in, where to store credentials safely, and how protected routes actually enforce access.
Authentication Patterns for Frontend Apps
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Session-based auth keeps the actual user state on the server and gives the browser only an opaque cookie that points to it, so the server can revoke access instantly by deleting that session.
- Token-based auth, usually with a JWT, packs the user's identity and claims directly into a signed token the client holds, which avoids a server-side lookup but makes revoking a single token before it expires much harder.
- OAuth is a protocol for letting a user grant one app limited access to their account on another service, without ever handing over their actual password to the first app.
- Where you store a token matters: `localStorage` is readable by any script on the page, which makes it vulnerable to theft via XSS, while an httpOnly cookie can't be read by JavaScript at all.
- A protected route isn't secure because the frontend hides a link to it — the frontend can only redirect or hide UI, so the real access check always has to happen on the server for every request.
The question underneath all of this: how does the server know it’s you?
Every request a frontend sends after login has to answer the same question the server keeps asking: who is this, and are they allowed to do what they’re asking to do? HTTP itself has no memory — every request arrives as a stranger. Authentication is the layer built on top to fix that, and it comes in two fundamentally different shapes. Understanding both, and how they connect to OAuth and to route protection, is what turns this from four disconnected topics into one coherent picture of how frontend auth actually works end to end.
Session-based auth: the server remembers, the browser carries a claim ticket
The older and conceptually simpler approach is the session. When you log in, the server creates a record — “user 42 is logged in” — stores it in a database or in memory, and gives the browser back a small opaque ID in a cookie. That cookie doesn’t contain any user information itself; it’s just a claim ticket. Every subsequent request includes the cookie, and the server looks up the matching session to find out who’s asking. Think of it like checking a coat: you get a numbered ticket, and the ticket itself tells the attendant nothing about your coat — they have to look it up. This means revoking access is trivial: delete the session record, and the ticket is now worthless, instantly, everywhere.
Token-based auth: the claim ticket is the ID card
The alternative is to skip the lookup entirely and have the token contain the identity information directly, cryptographically signed so it can’t be tampered with. This is what a JWT (JSON Web Token) is — a compact, signed packet of claims like “user 42, admin role, expires at 3pm.” The server verifies the signature and trusts the contents without touching a database. That’s faster and scales better across multiple servers with no shared session store, but it has a real cost: since the token is self-contained and valid until it naturally expires, there’s no simple “delete the record” move to revoke one early. Teams work around this with short expiry times plus a refresh mechanism, or a denylist for the rare case a token needs to die early — but it’s a genuine trade-off, not a strict upgrade over sessions.
OAuth: borrowing someone else’s authentication, safely
OAuth answers a different question: not “how does the server remember you,” but “how does one app let you grant a different app limited access to your account, without handing over your password.” When you click “log in with Google,” you’re redirected to Google, you authenticate there directly, and Google hands the requesting app back a scoped token — proof you authorized it, good for specific permissions, with your actual Google password never touching the requesting app at all. OAuth and the session-vs-token question aren’t competitors; OAuth is about how you establish who’s logging in, and sessions versus JWTs are about how the app remembers that afterward. A site can use OAuth to authenticate you and then issue its own session cookie or JWT for everything that follows.
Storing the credential safely
Wherever the frontend ends up holding a token or session identifier, storage matters. localStorage is plain JavaScript-accessible storage — any script running on the page, including an injected one from an XSS vulnerability, can read it and steal it. An httpOnly cookie, by contrast, is attached to requests automatically by the browser but is invisible to JavaScript entirely, which closes off that specific theft vector (though cookies bring their own concerns, like CSRF, covered in the next lesson). As a rule, prefer httpOnly cookies for anything sensitive, and treat localStorage tokens as a convenience with a real security cost attached.
Protected routes: the frontend is theater, the server is the lock
A “protected route” in a frontend app — a dashboard that redirects to /login if you’re not authenticated — is genuinely useful for user experience, but it provides zero actual security on its own. The frontend code runs entirely in the user’s browser, which means a user can simply call the underlying API endpoint directly, skipping the UI and its redirect entirely. The real protection has to live on the server: every request for protected data must independently check the session or token and reject anything that doesn’t check out. The frontend’s redirect is a convenience that keeps honest users from seeing a confusing error; the server’s check is the actual lock on the door.
Check yourself
What's the core structural difference between session-based and token-based authentication?
Session-based authentication stores the actual user state on the server and gives the browser a small opaque cookie, typically just an ID, that points to it. Token-based authentication packs identity information directly into a token, often a JWT, that the client holds and sends with each request, so the server can verify it without looking anything up.
Why is revoking access harder with token-based auth than with session-based auth?
A session can be revoked instantly by deleting the matching record on the server, since the cookie is meaningless without it. A signed token is self-contained and valid on its own until it expires, so the server has to either wait out the expiry or maintain a separate denylist of revoked tokens, which partly undoes the point of not needing a server-side lookup.
Why does OAuth let you "log in with Google" without the app you're logging into ever seeing your Google password?
OAuth works by redirecting you to the provider (Google) to authenticate directly with them, and the provider then hands the requesting app a token proving you authorized it, scoped to specific permissions. The app only ever sees that scoped token, never your actual Google credentials.
Why isn't hiding a link or redirecting away from a page enough to make a route "protected"?
The frontend only controls what's displayed in the browser, and a user can bypass a hidden link or client-side redirect by calling the underlying API directly. A route is only actually protected when the server checks the user's session or token on every request to that route's data and rejects unauthorized ones, regardless of what the frontend shows.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.