{"success":true,"name":"handle-session-expiry-and-401s-in-one-place-gracefully","description":"Users get stuck or see weird errors after being logged in for a while.","category":"ai-brimind","path":"ai-brimind/handle-session-expiry-and-401s-in-one-place-gracefully/SKILL.md","file":"SKILL.md","content":"---\nname: handle-session-expiry-and-401s-in-one-place-gracefully\ndescription: \"Users get stuck or see weird errors after being logged in for a while.\"\n---\n\n# Handle Session Expiry and 401s in One Place, Gracefully\n\n## When to use\nThe app uses sessions or tokens that expire, and each screen handles (or ignores) the expired state its own way.\n\n## Example request\nToken expires mid-use → every API call fails with cryptic errors; the UI shows broken data instead of 'please log in again' — unhandled expiry. A user fills a long form, submits, gets 401 → redirected to login, the form content is gone — expiry destroyed their work. Each fetch call checks 401 separately → half of them forget; behavior differs per screen — scattered auth handling.\n\n## Instructions\nAct like a developer who knows every session eventually expires at the worst possible moment. Centralize the handling: (1) route all API calls through one wrapper/interceptor where 401 is handled in exactly one place — detect it, and trigger a single consistent response: notify the user their session ended and route them to re-authenticate; scattered per-call handling guarantees inconsistency; (2) protect in-progress work — before redirecting to login, preserve what the user was doing (stash form state, remember the current page) and return them there after re-login; expiry should cost a login, not their last twenty minutes; (3) distinguish 401 (session invalid → re-authenticate) from 403 (valid session, forbidden action → explain, don't log them out); treating both as 'kick to login' punishes users for permission errors; (4) test it deliberately: expire/delete the token manually mid-session and walk the main flows. Only proceed once expiry anywhere in the app produces the same calm path: clear message, preserved work, easy re-entry.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #1165\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}