Sessions and tokens
Local fixture: Node.js 22.16.0, ECMAScript modules, synthetic data only. Run node main.mjs in a disposable directory. This is not a production security implementation.
Check expiry using a fixed clock
const sessions = new Map([['fixture-1',{user:'reader-a',expires:1000}]]);function lookup(id, now) { const session = sessions.get(id); return session && now < session.expires ? session.user : null;}console.log(lookup('fixture-1',999));console.log(lookup('fixture-1',1000));console.log(lookup('missing',0));Expected output:
reader-anullnullAt the expiry instant the identifier no longer resolves; missing identifiers also fail.
How it works
An opaque session identifier should be unpredictable and issued by a maintained session implementation. Rotate it when authentication or privilege changes, invalidate it on logout, and apply idle and absolute limits. Cookie attributes Secure, HttpOnly and SameSite reduce different risks; HttpOnly does not stop a browser sending cookies and SameSite is not a complete CSRF defense. JWTs are a format, not a login protocol; use a maintained verifier with trusted issuer, audience, allowed algorithms, key and expiry checks. Signed claims are not confidential by default. Revoking a self-contained access token immediately usually requires additional state or a revocation mechanism. Refresh tokens require a separate rotation and storage design. These examples model only session lookup and revocation with a fixed clock; literal identifiers are not real session IDs.
Rotate and revoke synthetic state
import assert from 'node:assert/strict';const sessions = new Map([['before-login',{user:null}]]);// Fixed labels model state transitions, not token generation.sessions.delete('before-login');sessions.set('after-login',{user:'reader-a'});assert.equal(sessions.has('before-login'),false);assert.equal(sessions.get('after-login').user,'reader-a');sessions.delete('after-login');assert.equal(sessions.has('after-login'),false);console.log('rotation and logout checks passed');Expected output:
rotation and logout checks passedDeleting the old mapping models server-side invalidation; a real multi-instance store needs atomic updates.
Common mistakes
Do not put credentials into URLs or logs. Do not equate base64 decoding with verification. Do not store a real session identifier in an exercise or use the following Map as a production session store.
Practice and next steps
Given an absolute expiry of 5000 and last activity at 1000, design a 1000-unit idle timeout. Test the exact boundary, a revoked session and a renewed session without extending absolute expiry.
Further reading: Primary documentation. Related staged reference: Session expiry and rotation.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Sessions and tokens — knowledge check
Apply the security boundary and distinguish the example from a production control.
Why rotate a session identifier after login?
What does decoding a JWT establish?
At the exact expiry boundary in this model, what happens?