{"success":true,"name":"design-the-full-life-of-a-login-expiry-refresh-and-logout-ar","description":"Building authentication and only the happy 'login works' path is planned","category":"ai-brimind","path":"ai-brimind/design-the-full-life-of-a-login-expiry-refresh-and-logout-ar/SKILL.md","file":"SKILL.md","content":"---\nname: design-the-full-life-of-a-login-expiry-refresh-and-logout-ar\ndescription: \"Building authentication and only the happy 'login works' path is planned\"\n---\n\n# Design the Full Life of a Login: Expiry, Refresh, and Logout Are Part of Auth, Not Afterthoughts\n\n## When to use\nThe app has (or is getting) login with tokens or sessions, and the design covers 'user logs in successfully' but not what happens when the token expires mid-use, how logout truly ends access, or how every screen reacts to a 401.\n\n## Example request\nToken works forever because no expiry was set → one leaked token equals permanent access to the account; expiry is what limits the damage of every leak, so a token without one is a design hole, not a convenience. Token expires after an hour but no plan exists for that moment → the user mid-work suddenly gets broken screens and cryptic errors; the app needed one global answer to 401: clean redirect to login (or a refresh flow), never a half-dead UI. Logout just deletes the token from the browser → fine for simple JWT setups if expiry is short, but anyone who copied the token still has it until expiry; know and SAY which guarantee your logout actually gives. Each screen handles expired sessions its own way → five different broken behaviors; 'what happens on 401' is decided once, in the shared API layer, this is the session lifecycle concept.\n\n## Instructions\nAct as an architect who designs authentication as a full life cycle — birth (login), life (valid requests), aging (expiry), renewal (refresh), and death (logout) — because 'login works' is one-fifth of the feature, and the other four-fifths are where accounts get stolen and users get stuck. Before calling auth done, do this: (1) Decide and write down the token/session lifetime and justify it: short lifetimes limit the blast radius of a leaked token; long ones are comfortable. If you want both, that is exactly what the refresh-token pattern is for (short-lived access token, longer refresh) — a named, solved pattern; check what your auth library (NextAuth, your JWT setup) already provides before hand-building it. (2) Script the expiry moment as a first-class scenario: the user is mid-action, a request returns 401 — what EXACTLY happens? The honest baseline: one central place (the shared API client or middleware) catches 401, clears local auth state, and routes to login with a human message ('session expired, please sign in') — never per-screen improvisation, never a page of dead buttons. If refreshing, the central place tries the refresh once, then retries or logs out. (3) Define what logout guarantees, truthfully: clearing the client token stops the browser; if you must be able to kill sessions server-side (stolen token, banned user, 'log out everywhere'), you need server-side session state or a token denylist — say whether your app needs that power or explicitly accept that it does not. (4) Check the storage and transport of the credential itself: httpOnly cookies vs localStorage each have named trade-offs — pick one on purpose and be consistent. (5) Walk the four flows end to end before shipping: fresh login, request with valid token, request with expired token, logout then attempted request. Only mark auth done when all four produce the exact behavior you scripted — an auth system tested only on the happy path is a lock tested only while open.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #210\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}