Skip to content
IT glossary
SecurityUpdated December 3, 2026

Cookies vs tokens

Cookie или токен

A cookie is a value the browser stores and automatically sends with matching requests. An access token is a credential a client presents to get access, whether in a cookie or through another channel such as an HTTP header.

Why this isn't an either-or choice

A cookie is a browser mechanism for storing and sending a value. A token is a credential the server can check. Put an access token in a cookie and you're using both. What people usually mean by “cookies vs tokens” is “a session ID sent in a cookie vs an access token sent in the Authorization header.” That comparison mixes two decisions: how the credential reaches the server and how the server decides whether to accept it.

Follow the request

With a server-side session, the server creates a session record after login and returns a random session ID in a Set-Cookie response header. The browser sends that ID in the Cookie header when the request matches the cookie's scope and sending rules. The server looks up the session and checks whether it is still valid and whether the user can perform the requested action.

With a bearer access token, the client typically adds Authorization: Bearer <access_token> itself. An opaque token is just a string to the client; the API can look it up or ask an authorization service whether it's active. A signed JWT can instead be validated locally by checking its signature and claims such as expiration, issuer, and audience. A signature provides integrity, not confidentiality, and a valid token doesn't necessarily grant access to every resource.

What changes for security?

Since the browser attaches cookies automatically, a cookie-based design must account for cross-site request forgery (CSRF). SameSite limits when cookies are sent in cross-site contexts. Depending on the application, state-changing requests may also need a CSRF token or an Origin check. Secure restricts cookie transmission to secure connections, and HttpOnly keeps JavaScript from reading the cookie. It does not stop script injected through cross-site scripting (XSS) from making authenticated requests in the user's browser.

A token added by application code to an authorization header isn't automatically attached to cross-site requests. But storing it in localStorage exposes it to JavaScript running on the page. If an attacker can run script through XSS, that script can read the token. The key question isn't just whether a request is possible, but whether the credential itself can be copied and used elsewhere.

How to discuss the trade-off in an interview

Start with the client. A same-origin web app is often well served by an HttpOnly session cookie. Mobile clients and service integrations commonly use authorization headers. Then explain logout: deleting a server-side session can invalidate its ID immediately, but clearing a JWT from one client doesn't invalidate other copies. If the API validates the JWT locally and has no revocation check, it can still accept that token until it expires. Be explicit about what the browser sends, what the API checks, and how quickly access must be revoked.