Skip to content

fix: restore SPA session on page refresh using refresh tokens and localstorage cache - #9

Merged
yogeshchoudhary147 merged 4 commits into
mainfrom
fix/spa-session-restoration
Aug 19, 2026
Merged

fix: restore SPA session on page refresh using refresh tokens and localstorage cache#9
yogeshchoudhary147 merged 4 commits into
mainfrom
fix/spa-session-restoration

Conversation

@yogeshchoudhary147

Copy link
Copy Markdown
Contributor

Problem

In SPA mode, authentication state was lost on every page refresh because:

  1. auth0-spa-js defaults to in-memory token storage — cleared on page refresh
  2. The hidden iframe silent auth fallback relies on third-party cookies, which modern browsers block by default

This caused users to be redirected to login on every page refresh or full-page navigation.

Fix

  • Default useRefreshTokens to true — uses refresh tokens instead of the blocked iframe approach
  • Default cacheLocation to localstorage — refresh token survives page refresh
  • Call getTokenSilently() during provider init to restore the session from the stored refresh token on remount

Both options are overridable via env vars for apps with stricter security requirements:

  • VITE_AUTH0_USE_REFRESH_TOKENS=false to disable refresh tokens
  • VITE_AUTH0_CACHE_LOCATION=memory to opt into in-memory only storage

Auth0 dashboard requirements

  • Allow Offline Access must be enabled on the API to issue refresh tokens
  • Allowed Web Origins must include the app origin

Comment thread src/client/Auth0Provider.tsx Outdated
Comment thread src/client/Auth0Provider.tsx Outdated
Comment thread src/client/Auth0Provider.tsx
@yogeshchoudhary147
yogeshchoudhary147 force-pushed the fix/spa-session-restoration branch 2 times, most recently from e52ddf4 to 0421c5b Compare August 18, 2026 13:35
@yogeshchoudhary147
yogeshchoudhary147 merged commit 202cb3c into main Aug 19, 2026
8 checks passed
@yogeshchoudhary147
yogeshchoudhary147 deleted the fix/spa-session-restoration branch August 19, 2026 04:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants