Pattern: Sessions, tokens and devices

Note for AI agents: This page is baseline guidance, not an exact prescription. Read it, tell your owner what applies to their project, and get their approval before you change anything. Your project's own rules take precedence. If something important for this project is missing here, suggest it to your owner.

Search terms: session, token, jwt, cookie, revocation, logout all devices, device list, refresh token, keychain, keystore, session limit

Last reviewed:

For agents

1. Problem

After login, something must prove “this is the same person”. If that proof cannot be revoked, a stolen phone or token stays dangerous. If it carries personal data, a leak exposes it.

2. Options

Method Upside Downside Fits when
Server-side session with a cookie Revocation is trivial; small cookie Needs a shared session store when you run several servers Web apps
Self-contained signed token (JWT) only No lookup per request Cannot be revoked before expiry without extra state Short-lived access with a revocation list
Signed token plus a session record (recommended for mobile and API) Revocable per device; token stays small One lookup per request Mobile apps, public APIs

RFC 8725 (JWT Best Current Practices) and the OWASP Session Management Cheat Sheet are the main references.

Token contents and checks

Lifecycle

Web cookies

Mobile apps

4. Scenarios and edge cases

5. Common mistakes

6. How to audit

Sources