Model session loading and expiry
A session is more than a signed-in boolean
Authentication establishes identity; a session lets the server recognize subsequent requests. Authorization is a separate decision about what that identity may do. In a fictional studio booking app, the initial browser state is loading, not signed out. A successful session lookup displays a minimal account summary. A missing session shows sign-in; a previously authenticated session that is no longer accepted shows an expiry notice. A failed network request is not proof of logout. Keep protected data out of the loading, expired and error views.
export function sessionView(status, account, hadSession = false) { if (status === 401) return {kind: hadSession ? 'expired' : 'signed-out'}; if (status === 403) return {kind: 'forbidden'}; if (status === 200 && typeof account?.displayName === 'string') { return {kind: 'signed-in', displayName: account.displayName}; } return {kind: 'error'};}A 200 response with a valid display name yields signed-in; a 401 yields expired or signed-out according to prior UI history. A 403 shows a denial rather than retrying sign-in automatically. A malformed successful response becomes an error. This deliberately small mapping accepts already parsed synthetic data, not a credential. The server alone determines whether the session is valid. Expiry is a UI interpretation, not a claim that the session timed out rather than being revoked.
Make session state available to descendants
import React, {createContext, useContext} from 'react';const SessionContext = createContext(null);export function SessionProvider({session, children}) { return <SessionContext.Provider value={session}>{children}</SessionContext.Provider>;}export function SessionPanel({onRetry}) { const session = useContext(SessionContext); if (!session) throw new Error('SessionProvider required'); switch (session.kind) { case 'loading': return <p role="status">Checking session...</p>; case 'signed-in': return <p>Welcome, {session.displayName}</p>; case 'expired': return <p>Session ended. <a href="/sign-in">Sign in again</a></p>; case 'signed-out': return <a href="/sign-in">Sign in</a>; case 'forbidden': return <p>Access is not permitted.</p>; default: return <button type="button" onClick={onRetry}>Retry session check</button>; }}Wrap this component in SessionProvider with a value such as {kind: "loading"}; replace that value with the result of sessionView after the lookup. The provider does not perform authentication. The sign-in path is a placeholder for a separately configured server flow. This uses the provider syntax shared by React 18 and 19. Do not initialize a trusted session from localStorage merely because it contains an account name.
Refresh and logout without trusting old responses
import {sessionView} from './session.mjs';export function createSessionLookup(readSession, publish) { let revision = 0; return { async refresh(hadSession = false) { const mine = ++revision; publish({kind:'loading'}); let view; try { const {status, account} = await readSession(); view = sessionView(status, account, hadSession); } catch { view = {kind:'error'}; } if (mine === revision) publish(view); }, invalidate() { ++revision; publish({kind:'signed-out'}); } };}Instantiate once per session owner, passing an injected lookup function and a state publisher. If a slow lookup completes after invalidate, its result is discarded. Invalidate is a local UI action, not server logout. A production integration also aborts obsolete work where practical, clears private caches, and calls the server to terminate the session. Refresh on relevant focus or navigation events according to product policy; avoid infinite retry loops. A 401 normally requires reauthentication unless an approved session renewal succeeds; coordinate renewal centrally, retry only appropriate requests and never blindly replay a mutation. On logout failure do not claim the server session ended. Practice: predict what the panel displays for a network failure versus a revoked session, then reason about an old response arriving after local invalidation.