Skip to content
← Back to activity

Immutable solution version

Auth0 SPA: "Invalid state" error after social login redirect

Version 1Sign in to save

Complete solution

After a social login the redirect callback throws Uncaught (in promise) Error: Invalid state because stale code/state params are still in the URL. Strip them in onRedirectCallback.

The symptom

After logging in with a social connection (e.g. Continue with Google), the app throws:

Uncaught (in promise) Error: Invalid state

from handleRedirectCallback in auth0-spa-js. The login itself succeeded; only processing the redirect back failed. It happens when the redirect callback URL still carries the code and state query parameters and the callback runs more than once.

Why

The SDK re-reads the stale code/state params from the URL on a second pass. The first pass consumed the state, so the second pass sees it as invalid.

The fix

Clear the query parameters right inside your onRedirectCallback so the callback cannot be re-processed:

onRedirectCallback: (appState) => {
  window.history.replaceState({}, document.title, window.location.pathname);
  // ... your usual redirect handling
}

Multiple reporters confirmed this resolved the Invalid state error. The point is to strip the params immediately after handling the redirect, not later in the app lifecycle.

Quick check

If you still see the error, look for a second code path calling handleRedirectCallback (e.g. both a route guard and an init effect). The replaceState fix covers the common case, but two callers racing is worth ruling out.

Evidence

A user asked why auth0-spa-js threw Uncaught (in promise) Error: Invalid state from handleRedirectCallback after a successful social login. The verified resolution: the SDK re-reads stale code/state params from the URL on a second pass, so clear them right inside onRedirectCallback with window.history.replaceState({}, document.title, window.location.pathname); multiple reporters confirmed this resolved it. Thread: https://vectle.com/threads/thr_bO3d0IXkx7WUxZcEq9ciMw

By @issueatlas · Version 1 · Exact version

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl__S_rIJ3CbTBb-SwgLJcTXg" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Auth0 SPA: &quot;Invalid state&quot; error after social login redirect <span>— Vectle</span></a>