Web6 min read10 interview questions
Cookies, sessions and tokens
How a website remembers who you are between clicks.
In one sentenceThe web forgets you after every request, so sites hand you a small proof of identity to show each time.
Think of it like thisIt is like a wristband at a festival: you prove who you are once at the gate, then just show the band.
The big picture
Tap any step to see where to use it, how to use it and an example result.
Cookies
A cookie is a small piece of text the server asks your browser to store and send back with later requests. It is the container, not the meaning. Cookies marked HttpOnly cannot be read by page scripts, which protects them from theft.
Sessions
With a session, the server keeps a record of who you are and gives you only a random ID. The ID is useless without the server's record, and the server can end it at any time by deleting the record.
Tokens
A token such as a JWT carries the facts inside it and is signed, so the server can trust it without looking anything up. It is handy across many servers, but hard to cancel before it expires, so keep tokens short-lived.
- Session: ID points to a record on the server
- Token: the proof itself is carried and signed
Common mistakes
- Storing tokens where page scripts can read them, such as localStorage, for high-value logins.
- Making tokens last for months.
- Putting private data inside a token. Anyone holding it can read the contents.
Quick recap
- HTTP forgets you, so sites give you proof to show
- A cookie is the container, a session or token is the proof
- Sessions can be cancelled easily
- Tokens are convenient but need short lifetimes
Try it yourself
Sign in to any site, open developer tools, go to Application, then Cookies, and look at which ones it set and whether they are marked HttpOnly and Secure.
Top 10 interview questions
Ordered from basic to advanced. Try answering out loud first, then open the answer.
What is a cookie?Basic
A small piece of data a website asks the browser to store and send back with later requests to that site. It is how a site recognises you between page loads.
What is a session?Basic
Server-side memory about a visitor. The browser holds only a random session ID in a cookie, and the server looks up the details, such as who is logged in, using that ID.
What is a token, such as a JWT?Basic
A signed piece of text that proves who you are and what you may do. The client sends it with each request, and the server checks the signature instead of looking anything up.
What is the difference between authentication and authorization?Basic
Authentication is proving who you are, for example by logging in. Authorization is deciding what you are allowed to do once your identity is known.
What do the HttpOnly, Secure and SameSite cookie flags do?Intermediate
HttpOnly hides the cookie from JavaScript, which limits theft through XSS. Secure sends it only over HTTPS. SameSite limits sending it on requests started from other sites, which reduces CSRF.
Sessions versus JWTs: what are the trade-offs?Intermediate
Sessions are easy to revoke because the server holds the state, but need shared storage when you have many servers. JWTs need no lookup and scale easily, but are hard to revoke before they expire, so they should be short-lived.
Where should a web app store a login token: localStorage or a cookie?Intermediate
An HttpOnly, Secure cookie is generally safer, because script injected by XSS cannot read it. localStorage is readable by any script on the page. A cookie then needs CSRF protection such as SameSite.
What is CSRF and how is it prevented?Intermediate
Cross-site request forgery tricks a logged-in user's browser into sending an unwanted request, because the browser attaches cookies automatically. Prevent it with SameSite cookies, anti-CSRF tokens and checking the request origin.
What are access tokens and refresh tokens?Advanced
An access token is short-lived and sent with each request. A refresh token is long-lived, stored more carefully and used only to get a new access token, so a stolen access token stops working quickly.
How do you log a user out everywhere or revoke a stolen token?Advanced
With sessions, delete the session on the server. With JWTs, keep a deny list or a token version number checked on each request, shorten token lifetimes, and rotate refresh tokens so reuse of an old one reveals theft.
Guides are simplified on purpose. Tool names and details change, so check the official documentation before relying on a detail.