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

How do expiration, refresh, and logout work?

THE IDEA

What it is

Expiration bounds credential lifetime. Refresh exchanges an eligible longer-lived credential for a new access credential. Logout terminates the chosen session lifecycle. Server sessions can be invalidated centrally; self-contained access tokens may remain usable until expiry unless validation also checks revocation state.

Why you need it

Deleting a browser value does not destroy a stolen copy. You need to state exactly how long access may persist and whether logout affects one device or every device.

How it works

  1. Validate the current session or refresh credential against its lifetime and revocation rules.
  2. On refresh, issue new credentials through an atomic rotation policy with replay handling.
  3. On logout, revoke the relevant server session or refresh family and clear the browser cookie.
  4. Reject future access according to the documented immediate or bounded-revocation guarantee.
Pseudocode

Revoking an opaque session

POST /logout:                       // CSRF-protected
  token = readSessionCookie(request)
  sessions.delete(hash(token))
  clearCookieWithMatchingScope()
  return 204

authenticate(token):
  session = sessions.find(hash(token))
  if missing or expired: reject
  return session.userId

A deleted session record makes an old opaque token unusable when every request checks the authoritative state. Cached state needs a compatible freshness rule. JWT access tokens need a separate revocation design if immediate logout is required.

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

  • Use server-enforced idle and absolute limits where appropriate, with a deliberate remember-me policy.
  • Handle simultaneous refresh attempts without accidental duplicate issuance, and make logout safe to repeat.

Common mistakes

  • Issuing a refresh token that never expires or can be reused indefinitely without detection.
  • Claiming immediate logout while checking only an access token’s signature and expiry.
GUIDED IMPLEMENTATION

Try it yourself

Use the lab to create two sessions. Log out one and verify the other still works. Explain how you would implement logout everywhere.

What a successful result looks like

The logged-out token is rejected, the second session remains active, and your explanation distinguishes device-specific from account-wide revocation.

0 / 6000 characters · Saved on this device. Do not include passwords or private data.

Check your understanding

Only the refresh token was revoked. What can happen to an already issued access JWT?

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