{"success":true,"name":"store-and-send-tokens-the-safe-way-not-the-convenient-way","description":"Deciding where the frontend keeps auth tokens like JWTs","category":"ai-brimind","path":"ai-brimind/store-and-send-tokens-the-safe-way-not-the-convenient-way/SKILL.md","file":"SKILL.md","content":"---\nname: store-and-send-tokens-the-safe-way-not-the-convenient-way\ndescription: \"Deciding where the frontend keeps auth tokens like JWTs\"\n---\n\n# Store and Send Tokens the Safe Way, Not the Convenient Way\n\n## When to use\nThe frontend must hold a token or session credential after login, and you are choosing localStorage, cookies, or memory.\n\n## Example request\nJWT saved in localStorage 'because it's easy' → any XSS on the page can read and exfiltrate it; HttpOnly cookies are unreadable to scripts (XSS-resistant storage). Access token with a 30-day lifetime → one stolen token means a month of access; short-lived access plus a refresh mechanism limits the damage (token lifetime). Token payload trusted by the frontend for role checks only → the payload is readable and the server must still verify the signature and claims on every request (verify server-side). Tokens appended to URLs for convenience → URLs land in history, logs, and referrer headers; tokens travel in headers or cookies, never in the address bar (leaky channels).\n\n## Instructions\nAct like a developer who treats the auth token as the account itself, choosing its home by threat, not convenience. Compare the storage options honestly: anything JavaScript can read (localStorage, sessionStorage, plain JS variables exposed globally) is stolen the moment any XSS runs on the page, while HttpOnly, Secure, SameSite cookies stay out of script reach — so prefer the cookie approach for browser apps unless a strong documented reason exists, and pair it with the framework's CSRF protection since cookies auto-attach. Keep access tokens short-lived and use the platform's refresh mechanism so a stolen token expires quickly; make logout and password change revoke refresh ability server-side. Never put tokens in URLs, and never let the server trust a token's readable payload without verifying its signature, expiry, and audience on every request using the library's verification function — decoding is not verifying. If an auth provider or framework session system already manages all this, use it instead of assembling the pieces manually. Only ship once you can state where the token lives, how long it lives, how it dies, and which theft path each choice blocks — not just that login persists across refresh.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #548\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}