Session lifecycle
What you need to know
Authentication establishes who is making a request; a session represents that identity across requests. Authorization is a separate decision about what the identified user may access.
By the end, you can: distinguish authentication and authorization; explain expiry.
Classify an already looked-up synthetic session
type Session={userId:string;expiresAt:number;revoked:boolean};function principal(session:Session|null,now:number) { if(!session || session.revoked || session.expiresAt<=now) return null; return {id:session.userId};}console.log(principal({userId:'reader-a',expiresAt:2000,revoked:false},1999));console.log(principal({userId:'reader-a',expiresAt:2000,revoked:false},2000));Expected behavior: An identity object, then null. The expiration boundary is exclusive. This assumes a trusted lookup and does not authenticate a raw token.
How it works
A session may refer to a server-side record using an opaque identifier, or use a maintained library’s signed/encrypted session format. The server must validate the session, expiry, revocation and intended application before trusting identity. For a server-side record, logout can revoke the record and expire its cookie. Rotate identifiers after login and privilege changes; use an established authentication/session library rather than inventing a token protocol. Password storage should use maintained password-hashing libraries with appropriate parameters, not plain text or general-purpose hashes. OAuth is delegated authorization; OpenID Connect adds an identity layer. Use maintained integrations that validate issuer, audience, signature, state/nonce and applicable PKCE requirements rather than building callback verification yourself. This batch uses injected synthetic session records and a fixed clock; it never creates real login credentials, cookies containing tokens or identity-provider accounts.
An authorization decision remains separate
type Principal={id:string};function mayRead(user:Principal|null,book:{ownerId:string}) { return user!==null && user.id===book.ownerId;}console.log(mayRead({id:'reader-a'},{ownerId:'reader-b'}));Expected behavior: false: an authenticated reader is not entitled to another reader’s book.
Common mistake
Do not treat the presence of a cookie or a client-supplied userId as authentication. Do not assume authenticated users can access every record. Use trusted server-side session verification and resource checks.
Practice and verification
Model missing, expired, revoked and active sessions at a fixed time. Compare a cookie-only session with server-side revocation requirements. For a real app evaluate a maintained authentication library’s current compatibility and security policy before selecting it.
Scope: App Router, proposed Next.js 16.x, React 19 family, Cache Components disabled. Framework snippets are static or explicitly partial; actual browser, auth and generated-route checks are pending. SQLite exercises run separately in the authoring environment, not the ByteMind evaluator.