LEARN. PRACTICE. EXPLAIN.
Learning paths & interview practiceP
Backend foundations
LESSON 4 OF 8 · ABOUT 15 MINUTES + EXERCISE

How do you stay logged in after a refresh?

THE IDEA

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

  1. The login response sets a cookie.
  2. The browser attaches it to matching requests according to cookie rules.
  3. GET /me validates the session and returns a minimal current-user response.
  4. The frontend shows signed-in, signed-out, or temporarily unavailable state based on the result.
HTTP

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.

Try the local authentication lab

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.

Download runnable lab

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.
GUIDED IMPLEMENTATION

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

The browser retains a cookie after the server revokes its session. Is the caller still authenticated?

Answer the understanding check correctly to record exercise completion.

PUT THE IDEA UNDER PRESSURE

Related interview practice

These scenarios add constraints and follow-up questions to the concepts you just learned.

Further reading

Selected primary references · reviewed 2026-10-09. Examples are educational; use your framework’s documented integration guidance when building a deployed system.

AlifBay Prep