What it is
The browser can send a stored session cookie with later matching requests. After a refresh, the frontend asks the server who is signed in, rather than relying on a JavaScript variable that was erased. Browser cookie lifetime and server session validity are related but separate decisions.
Why you need it
A page reload should not require a password every time, but a saved isLoggedIn flag must not become proof of authorization. The server remains the authority on whether the session is valid.
How it works
- The login response sets a cookie.
- The browser attaches it to matching requests according to cookie rules.
- GET /me validates the session and returns a minimal current-user response.
- The frontend shows signed-in, signed-out, or temporarily unavailable state based on the result.
An illustrative production cookie
Set-Cookie: __Host-session=<opaque-value>; Path=/; Secure; HttpOnly; SameSite=Lax; Max-Age=3600
GET /me
Cookie: __Host-session=<opaque-value>
HTTP/1.1 200 OK
Cache-Control: no-store
{"id":"user-7","displayName":"Learner"}Secure limits transmission to secure contexts, HttpOnly prevents ordinary JavaScript cookie reads, and SameSite influences cross-site sending. This cookie uses no Domain attribute. The server must still enforce its own expiry and permissions. The example is HTTP notation, not a complete endpoint.
Download the file and run node backend-auth.mjs with Node.js 22 or newer. It tests an in-memory auth model; it is not a production login server.
Good practices and why
- Choose cookie scope and lifetime deliberately, enforce HTTPS in production, and distinguish authentication failure from a temporary service error.
- Protect state-changing cookie-authenticated requests against CSRF; SameSite and HttpOnly are not universal substitutes for a complete defense.
Common mistakes
- Persisting only isLoggedIn=true and trusting it on the server.
- Assuming HttpOnly stops injected JavaScript from performing actions as the user.
Try it yourself
In a local application you control, inspect a session cookie and the /me request using browser developer tools. Explain what should happen after reload, expiry, and a network failure.
What a successful result looks like
You distinguish lost client state, invalid server sessions, and unknown network outcomes; none is resolved by trusting a client boolean.
0 / 6000 characters · Saved on this device. Do not include passwords or private data.
Check your understanding
Answer the understanding check correctly to record exercise completion.
Related interview practice
- The cookie is secure. Is the action safe? ↗
- The API works in a command-line client but fails in the browser. ↗
These scenarios add constraints and follow-up questions to the concepts you just learned.
Further reading
- OWASP: authentication ↗
- OWASP: password storage ↗
- OWASP: session management ↗
- MDN: Set-Cookie ↗
- IETF: JWT best current practices ↗
Selected primary references · reviewed 2026-10-09. Examples are educational; use your framework’s documented integration guidance when building a deployed system.