Cookie security and logout
What you need to know
A session cookie is sent with matching HTTP requests. Cookie flags reduce particular risks, while the actual session verification and revocation policy remain server responsibilities.
By the end, you can: select cookie protections; define session invalidation.
Review a production session-cookie policy
// Policy fragment for an approved HTTPS auth integration; no token issuedconst sessionCookieOptions = { httpOnly:true, secure:true, sameSite:'lax' as const, path:'/', maxAge: 15*60};console.log(sessionCookieOptions.maxAge);Expected behavior: 900 seconds. The actual auth library must enforce a compatible server session lifetime and revocation policy.
How it works
Use HttpOnly for session cookies to limit access from client-side script, Secure for HTTPS-only transport, an appropriate SameSite policy for the application's cross-site flows, narrowly scoped path/domain, and a bounded lifetime. HttpOnly does not stop scripts from initiating authenticated requests. SameSite reduces some cross-site request risks but does not replace a complete CSRF policy. A cookie may disappear locally while a copied server-side session identifier still works unless revoked. Conversely, an expired or revoked record must be rejected even if a browser retains its cookie. In App Router, await cookies() reads incoming values; changing cookies requires a Server Function or Route Handler before response streaming commits headers. Keep session IDs out of URLs, logs and Client Component props. Examples below show option selection and a harmless preference cookie deletion, not a homemade authentication protocol.
Clear a harmless preference in a Route Handler
// app/api/clear-preference/route.ts — not session logoutimport {cookies} from 'next/headers';export async function POST() { const jar=await cookies(); jar.delete('reading-theme'); return new Response(null,{status:204});}Expected behavior: The response instructs the browser to delete the preference cookie for the matching scope. Use the same domain and protocol as the original cookie, and verify its path and domain attributes in the browser; this example assumes the default root path. No session or database state is changed.
Common mistake
Do not implement logout as hiding the dashboard or removing a client state value. For server-stored sessions revoke the session through the provider, expire the cookie with matching scope, and reject subsequent requests. A cookie deletion alone is not general server-side revocation.
Practice and verification
Inspect the option policy and describe which risk each flag reduces. Design a logout test that reuses a synthetic session lookup after revocation and expects rejection. In a real HTTPS fixture verify actual Set-Cookie behavior and browser scope.
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.