[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"blog-en-spa-api-session-refresh-patterns":3},{"id":4,"title":5,"author":6,"body":7,"cover":367,"description":368,"draft":369,"extension":370,"locale":371,"meta":372,"navigation":373,"path":374,"publishedAt":375,"seo":376,"seoTitle":377,"slug":378,"stem":379,"tags":380,"__hash__":382},"blog\u002Fen\u002Fblog\u002Fspa-api-session-refresh-patterns.md","Sessions and refresh tokens in SPAs: patterns and common mistakes","Manuel Tardivo",{"type":8,"value":9,"toc":356},"minimark",[10,32,58,63,109,113,157,173,177,203,207,218,241,245,255,259,275,306,309,313,330,334,352],[11,12,13,14,18,19,22,23,27,28,31],"p",{},"A SPA (React, Vue, Svelte…) calling your API must place ",[15,16,17],"strong",{},"short-lived access tokens"," and ",[15,20,21],{},"long-lived refresh tokens"," somewhere. Decks say “use HttpOnly cookies”; in production many teams drop JWTs into ",[24,25,26],"code",{},"localStorage"," for convenience—and hand XSS ",[15,29,30],{},"long-lived"," power.",[11,33,34,35,42,43,42,48,53,54,57],{},"This article is framework-agnostic; wire it with ",[36,37,41],"a",{"href":38,"rel":39},"https:\u002F\u002Fauthjs.dev\u002F",[40],"nofollow","Auth.js",", ",[36,44,47],{"href":45,"rel":46},"https:\u002F\u002Flucia-auth.com\u002F",[40],"Lucia",[36,49,52],{"href":50,"rel":51},"https:\u002F\u002Fwww.better-auth.com\u002F",[40],"better-auth"," or bespoke sessions—the point is the ",[15,55,56],{},"threat model",", not the vendor badge.",[59,60,62],"h2",{"id":61},"core-threats","Core threats",[64,65,66,84],"ul",{},[67,68,69,72,73,75,76,79,80,83],"li",{},[15,70,71],{},"XSS",": hostile JS reads ",[24,74,26],{}," \u002F ",[24,77,78],{},"sessionStorage",". HttpOnly cookies are ",[15,81,82],{},"not"," script-readable → preferred for refresh (and often session ids).",[67,85,86,89,90,93,94,97,98,93,101,104,105,108],{},[15,87,88],{},"CSRF",": browsers auto-send cookies. Mitigations: ",[24,91,92],{},"SameSite=Lax"," or ",[24,95,96],{},"Strict"," for typical navigations, ",[15,99,100],{},"CSRF tokens",[15,102,103],{},"double-submit"," cookies when you must use ",[24,106,107],{},"SameSite=None",", or avoid cookies on cross-origin APIs and rely on bearer tokens in memory (worsens XSS urgency).",[59,110,112],{"id":111},"recommended-high-level-pattern","Recommended high-level pattern",[114,115,116,129,151],"ol",{},[67,117,118,121,122,125,126,128],{},[15,119,120],{},"Access token",": 5–15 minute TTL, kept in ",[15,123,124],{},"JS memory"," (volatile store) when possible. If you need cross-tab continuity, prefer an API-wrapped session cookie over ",[24,127,26],{},".",[67,130,131,134,135,138,139,142,143,146,147,150],{},[15,132,133],{},"Refresh token",": ",[15,136,137],{},"HttpOnly + Secure + tight path"," ",[24,140,141],{},"\u002Fauth\u002Frefresh",", renewed via dedicated POST. ",[15,144,145],{},"Rotate"," every refresh: issue a new refresh server-side and invalidate the previous token (",[24,148,149],{},"jti"," table or session row).",[67,152,153,156],{},[15,154,155],{},"Revocation",": server-side version or denylist so a stolen refresh dies after one round trip.",[158,159,164],"pre",{"className":160,"code":161,"language":162,"meta":163,"style":163},"language-http shiki shiki-themes github-light github-dark","Set-Cookie: refresh=...; HttpOnly; Secure; Path=\u002Fauth\u002Frefresh; SameSite=Lax; Max-Age=...\n","http","",[24,165,166],{"__ignoreMap":163},[167,168,171],"span",{"class":169,"line":170},"line",1,[167,172,161],{},[59,174,176],{"id":175},"avoid","Avoid",[64,178,179,187,193],{},[67,180,181,186],{},[15,182,183,184],{},"Refresh tokens in ",[24,185,26],{}," unless the threat model explicitly allows it.",[67,188,189,192],{},[15,190,191],{},"Long access tokens"," “to reduce refresh traffic”—they widen the blast radius on device compromise.",[67,194,195,198,199,202],{},[15,196,197],{},"Concurrent refresh storms"," without deduplication—four tabs can mint four sessions; share a ",[24,200,201],{},"refreshPromise"," mutex or make the endpoint idempotent.",[59,204,206],{"id":205},"spa-api-on-different-hosts","SPA + API on different hosts",[11,208,209,210,213,214,217],{},"If the UI is ",[24,211,212],{},"app.example.com"," and API ",[24,215,216],{},"api.example.com",":",[64,219,220,234],{},[67,221,222,223,226,227,226,230,233],{},"Cross-subdomain cookies: ",[24,224,225],{},"Domain=.example.com"," + ",[24,228,229],{},"Secure",[24,231,232],{},"SameSite"," aligned with your OAuth\u002FPKCE flow.",[67,235,236,237,240],{},"Alternative: ",[15,238,239],{},"Backend-for-Frontend"," on the same site as the UI to proxy—cuts CORS\u002Fcookie pain.",[59,242,244],{"id":243},"logout","Logout",[11,246,247,248,251,252,254],{},"Clear refresh cookies (",[24,249,250],{},"Max-Age=0","), invalidate ",[24,253,149],{}," server-side, wipe in-memory client state. Client-only logout without an API call leaves a valid refresh behind.",[59,256,258],{"id":257},"rfc-9700-bcp-240-refresh-tokens-for-public-clients","RFC 9700 (BCP 240): refresh tokens for public clients",[11,260,261,266,267,271,272,217],{},[36,262,265],{"href":263,"rel":264},"https:\u002F\u002Fwww.rfc-editor.org\u002Finfo\u002Frfc9700",[40],"RFC 9700"," (",[268,269,270],"em",{},"OAuth 2.0 Security BCP",", Jan 2025) is the updated guidance auditors expect. The actionable bit is ",[15,273,274],{},"refresh-token replay detection",[64,276,277,294],{},[67,278,279,282,283,286,287,290,291,128],{},[15,280,281],{},"Public clients"," (typical browser SPA without a client secret) require either ",[15,284,285],{},"refresh-token rotation"," or sender-constrained refresh tokens (DPoP \u002F mTLS). With rotation, each refresh issues a ",[15,288,289],{},"new"," refresh token and invalidates the prior one; concurrent reuse means someone presents an already-invalidated token, letting the AS ",[15,292,293],{},"detect compromise and revoke the grant chain",[67,295,296,297,266,300,305],{},"Useful pull quote: ",[268,298,299],{},"“If a refresh token is compromised and subsequently used by both the attacker and the legitimate client, one of them will present an invalidated refresh token…”",[36,301,304],{"href":302,"rel":303},"https:\u002F\u002Fwww.rfc-editor.org\u002Frfc\u002Frfc9700.html",[40],"RFC 9700 §4.14",").",[11,307,308],{},"Major IdPs encode these rules in their SPA integrations—custom stacks should too.",[59,310,312],{"id":311},"references","References",[11,314,315,42,320,323,324,329],{},[36,316,319],{"href":317,"rel":318},"https:\u002F\u002Fcheatsheetseries.owasp.org\u002Fcheatsheets\u002FSession_Management_Cheat_Sheet.html",[40],"OWASP Session Management Cheat Sheet",[36,321,265],{"href":263,"rel":322},[40],", and ",[36,325,328],{"href":326,"rel":327},"https:\u002F\u002Fwww.rfc-editor.org\u002Frfc\u002Frfc6749",[40],"RFC 6749"," refresh semantics—adapt if you ship custom sessions instead of raw OAuth grants.",[59,331,333],{"id":332},"summary","Summary",[11,335,336,42,339,42,342,42,345,348,349,351],{},[15,337,338],{},"HttpOnly refresh",[15,340,341],{},"short in-memory access",[15,343,344],{},"rotate + revoke server-side",[15,346,347],{},"dedup refresh",". ",[24,350,26],{}," for long-lived secrets remains the most expensive shortcut we see in the field.",[353,354,355],"style",{},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":163,"searchDepth":357,"depth":357,"links":358},2,[359,360,361,362,363,364,365,366],{"id":61,"depth":357,"text":62},{"id":111,"depth":357,"text":112},{"id":175,"depth":357,"text":176},{"id":205,"depth":357,"text":206},{"id":243,"depth":357,"text":244},{"id":257,"depth":357,"text":258},{"id":311,"depth":357,"text":312},{"id":332,"depth":357,"text":333},"\u002Fblog\u002F33_spa_api_session_refresh_patterns_1x.webp","Where to store session and refresh tokens in an SPA, how to rotate them, and which threats are realistic with cookies or bearer tokens to your API.",false,"md","en",{},true,"\u002Fen\u002Fblog\u002Fspa-api-session-refresh-patterns","2026-05-26",{"title":5,"description":368},"SPA sessions and refresh token mistakes","spa-api-session-refresh-patterns","en\u002Fblog\u002Fspa-api-session-refresh-patterns",[381],"mvp","DvnFaY1HoU0T6hrD7EtJi3yX_keOpoRM4nEgFnnB3zw"]