How Web Authentication Works: Cookies, Sessions, Tokens, and OAuth

Fractional CTO · Founder of Injuria.ai
Every time you stay logged in to a website, something is quietly working around a core fact of the web: HTTP has no memory. Each request arrives at the server as if it were the first one it had ever seen. The server does not know that the request asking for your account page is the same person who typed a password a moment ago. Authentication is the set of tricks we use to bridge that gap, and understanding the pieces makes a surprising amount of web security click into place.[9]
Authentication is not authorization
These two words get used interchangeably, and they are not the same thing. Authentication answers "who are you?" It is the login step, where you prove your identity with a password, a passkey, or a code from your phone. Authorization answers a different question, "what are you allowed to do?" It is the check that happens after you are known, deciding whether you can see the admin dashboard or only your own profile.
A system can get authentication right and authorization wrong. Letting a logged-in user view another user's invoices by changing a number in the URL is an authorization bug, not an authentication one. Keeping the two ideas separate in your head is the first step to reasoning about either.
The real problem: HTTP forgets
Because HTTP is stateless, the server needs the client to carry some proof of identity on every single request.[1] The entire design space of web authentication comes down to one question: what is that proof, and where do we keep it?
There are two broad answers. The server can hand the client a meaningless ticket and remember the details itself. Or the server can hand the client a signed document that contains the details, and trust the signature later. The first is a session. The second is a token. Almost everything else is a variation on those two.
Cookies and server-side sessions
The oldest and still most common approach is the server-side session. When you log in, the server creates a session record, stores it somewhere it controls (memory, a database, or a cache like Redis), and generates a random unguessable session ID. It sends that ID back to the browser as a cookie.[1]
Set-Cookie: session=9f2a1c8e...; HttpOnly; Secure; SameSite=Lax
A cookie is just a small piece of data the browser stores and automatically attaches to future requests for that site. On every later request, the browser sends the session cookie, the server looks up the matching record, and it knows who you are. The cookie itself contains nothing sensitive. It is a claim check, and the real information stays on the server.
The flags on that cookie do the security work. HttpOnly prevents JavaScript from reading it, which blunts a large class of theft attacks. Secure means it only travels over HTTPS. SameSite controls whether the cookie rides along on requests coming from other sites, which is the main defense against cross-site request forgery. It is worth knowing the three values: Strict never sends the cookie on a cross-site request, Lax sends it only on top-level navigations that use safe HTTP methods (like GET), preventing it from being sent on cross-site POST requests, and is now the browser default, and None sends it everywhere but requires Secure. One more session rule matters more than people expect: rotate the session ID the moment someone logs in. Skipping that opens a session fixation attack, where an attacker plants a known session ID before login and inherits it afterward.[8][10]
The strength of sessions is control. If you need to log someone out everywhere, you delete the record and the ticket is instantly worthless. The cost is that the server has to store and look up state for every active user, which takes a little more infrastructure as you grow.
Stateless tokens and JWTs
The other approach flips the arrangement. Instead of keeping the details on the server, you put them in the token and sign it. The common format is the JSON Web Token, or JWT.[2] A JWT holds a small JSON payload, your user ID, maybe your role, an expiry time, and a signature the server can verify without looking anything up.
{ "sub": "user_123", "role": "member", "exp": 1735689600 }
A point of precision that trips people up: JWT is an umbrella term. The kind almost everyone uses is a signed JWT, technically a JSON Web Signature, whose payload is only encoded, not encrypted, so anyone holding it can read the contents.[3] There is also an encrypted variant, JSON Web Encryption, which hides the payload.[4] When someone says "a JWT is readable by anyone," they mean the common signed kind, and the practical rule stands: never put a secret in a signed token.
Because the signature proves the token has not been tampered with, any server that knows the signing key can trust it on sight, with no database lookup, which is appealing for systems spread across many services. The trade-off is the one people underestimate. A signed token is valid until it expires, and you cannot easily revoke it in the meantime, because there is no central record to delete. If a token is stolen, it works until it times out. The usual fixes reintroduce exactly the state you were trying to avoid: keep access tokens short-lived and pair them with a revocable refresh token, or check each token against a denylist or a per-user "token version" in the database. The moment you need real revocation, the stateless dream leaks a little state back in.
One more practical warning the hype skips: where you store the token on the client is a security decision. Put a JWT in localStorage and any cross-site scripting flaw on your page can read it and walk away with the user's identity.[11] A cookie with HttpOnly cannot be read by script at all. The irony is that "modern" token auth often ends up stored in the same cookies the "old" sessions used, for exactly this reason.
OAuth and OpenID Connect
OAuth solves a question the previous methods do not: how do you let one application act on your behalf at another without handing over your password? When an app offers "continue with Google," the identity part you are using is OpenID Connect, a layer built on top of OAuth, and that distinction matters. OAuth 2.0 on its own is about authorization, granting scoped access to resources.[5] OpenID Connect is the thin layer that adds authentication, a standard way to answer "who is this person?"[6] It returns an ID token, a signed JWT describing the user, which is what "sign in with" buttons actually rely on.
The flow has variations you should know exist, because using the wrong one is a real vulnerability.[12] Under OAuth 2.1 and current Best Current Practices, all of these app types use the same flow: the Authorization Code flow with PKCE, an extension that stops an intercepted authorization code from being redeemed by an attacker.[7] The practical benefit of the whole arrangement is that you offload the hardest parts of security, password storage and account recovery, to a provider that specializes in it, and the app never sees the user's Google password.
The quiet fact underneath all of it
For a single application with its own users, server-side sessions with a secure cookie are the boring, correct default. They are simple to reason about, easy to revoke, and hard to get dangerously wrong. Stateless tokens earn their keep when you genuinely have many independent services that must verify identity without a shared store, and they cost you easy revocation to get there. OAuth and OpenID Connect are the standard for letting users bring an existing account, and using a reputable provider is almost always safer than building it yourself. My blunt advice after watching teams overcomplicate this: reach for a session cookie first, and only adopt tokens when you can name the specific distributed-systems problem they solve, because most of the JWT security incidents I have seen started with someone choosing tokens because they sounded modern. The web forgets you on every request, and authentication is the ongoing work of helping it remember just enough, for just long enough, without leaving the door open.
Sources (12)
- RFC 6265: HTTP State Management Mechanism (cookies)
- RFC 7519: JSON Web Token (JWT)
- RFC 7515: JSON Web Signature (JWS)
- RFC 7516: JSON Web Encryption (JWE)
- RFC 6749: The OAuth 2.0 Authorization Framework
- OpenID Connect Core 1.0
- RFC 7636: Proof Key for Code Exchange (PKCE)
- OWASP Session Management Cheat Sheet
- Carlos Serrao, Vicente Aguilera, Fabio Cerullo, Web Application Security (Springer Science & Business Media, 2010)
- Marcello Zillo Neto, Gustavo A. A. Santana, Fernando Sapata, AWS Certified Security Study Guide (John Wiley & Sons, 2020)
- Ronald L. Krutz, Alexander J. Fry, The CSSLP Prep Guide (John Wiley & Sons, 2009)
- Ali Abdollahi, A Beginner's Guide To Web Application Penetration Testing (John Wiley & Sons, 2025)