# handleRedirectCallback failing with "Invalid State" error & empty response

**URL:** <https://community.auth0.com/t/handleredirectcallback-failing-with-invalid-state-error-empty-response/86333>\
**Category:** Get Help\
**Created:** [July 3, 2022, 3:34pm UTC](https://community.auth0.com/t/handleredirectcallback-failing-with-invalid-state-error-empty-response/86333 "2022-07-03T15:34:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![SreekarMouli98](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/sreekarmouli98/32/38038_2.png) [@SreekarMouli98](https://community.auth0.com/u/SreekarMouli98)\
**Post date:** [July 3, 2022, 3:34pm UTC](https://community.auth0.com/t/handleredirectcallback-failing-with-invalid-state-error-empty-response/86333/1 "2022-07-03T15:34:24Z")

</div>

I am using the `@auth0/auth0-spa-js` module in my reactjs application.

I am using `loginWithRedirect` and as per the doc, I have to call `handleRedirectCallback` after we are redirected from Auth0 back to SPA.

So, in `App` component, I have used an `useEffect` listening to the query params. If `code` query param is present in the URL, then I am calling `handleRedirectCallback`.

**Code:**

```jsx
import createAuth0Client from "@auth0/auth0-spa-js";
import { useEffect, useState } from "react";
import { isEmpty } from "lodash";
import { BrowserRouter, useSearchParams } from "react-router-dom";

function LoginButton({ auth0, setUser }) {
  return (
    <>
      <button
        onClick={async () => {
          await auth0.loginWithRedirect({
            redirect_uri: "http://localhost:3000/",
          });
          // await auth0.loginWithPopup();
          const _user = await auth0.getUser();
          setUser(_user);
        }}
      >
        LOGIN
      </button>
    </>
  );
}

function App({ auth0, user, setUser }) {
  const [searchParams] = useSearchParams();

  useEffect(() => {
    (async () => {
      if (searchParams && searchParams.get("code")) {
        let code = searchParams.get("code");
        console.log("App -> code", code);
        let res = await auth0.handleRedirectCallback();
        console.log("App -> handleRedirectCallback -> res", res);
      }
    })();
  }, [auth0, searchParams]);

  return (
    <>
      <h1>APP</h1>
      <hr />
      {isEmpty(user) && <LoginButton auth0={auth0} setUser={setUser} />}
      {!isEmpty(user) && (
        <>
          <Profile auth0={auth0} user={user} />
        </>
      )}
      <LogoutButton auth0={auth0} setUser={setUser} />
    </>
  );
}

function AppWrapper() {
  const [auth0, setAuth0] = useState();
  const [user, setUser] = useState();

  useEffect(() => {
    (async () => {
      const _auth0 = await createAuth0Client({
        domain: "DOMAIN,
        client_id: "CLIENT-ID",
        cacheLocation: "localstorage",
        useRefreshTokens: true,
      });
      setAuth0(_auth0);
      const _user = await _auth0.getUser();
      console.log("AppWrapper -> _user", _user);
      setUser(_user);
    })();
  }, []);

  if (!auth0) {
    return (
      <>
        <p>Loading...</p>
      </>
    );
  }

  return (
    <>
      <BrowserRouter>
        <App auth0={auth0} user={user} setUser={setUser} />
      </BrowserRouter>
    </>
  );
}

export default AppWrapper;

```

I am getting the following error in the console along with an empty response in `appState`:

> App → code xxxxxxxxxxxx
> 
> Auth0Client.ts:668 Uncaught (in promise) Error: Invalid state  
> at e. (Auth0Client.ts:668:1)  
> at tslib.es6.js:102:1  
> at Object.next (tslib.es6.js:83:1)  
> at tslib.es6.js:76:1  
> at new Promise ()  
> at o (tslib.es6.js:72:1)  
> at e.handleRedirectCallback (Auth0Client.ts:660:1)  
> at App.js:75:1  
> at App.js:78:1  
> at commitHookEffectListMount (react-dom.development.js:23150:1)
> 
> App → handleRedirectCallback → res {appState: undefined}

---

<div class="post-metadata">

**Author:** ![andrew.gonsalves](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/andrew.gonsalves/32/40504_2.png) [@andrew.gonsalves](https://community.auth0.com/u/andrew.gonsalves)\
**Post date:** [October 10, 2022, 6:24pm UTC](https://community.auth0.com/t/handleredirectcallback-failing-with-invalid-state-error-empty-response/86333/2 "2022-10-10T18:24:26Z")

</div>

Did you ever resolve this? I am running into the same issue, but with NextJS.

---

<div class="post-metadata">

**Author:** ![TheMuffinMan](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/themuffinman/32/37632_2.png) [@TheMuffinMan](https://community.auth0.com/u/TheMuffinMan)\
**Post date:** [February 19, 2025, 4:34am UTC](https://community.auth0.com/t/handleredirectcallback-failing-with-invalid-state-error-empty-response/86333/3 "2025-02-19T04:34:25Z")

</div>

I don’t know why you’re running into this issue specifically, but I can give you the information necessary to troubleshoot it.

If we go inspect the source code for `loginWithRedirect` and `handleRedirectCallback` we can observe that it internally uses a Transaction Manager and a helper function to store a session storage object with some random values. In the latter method it then checks these values to ensure they are present/valid.

If you look at all of the places in the source code where you get that “Invalid State” error you can see what conditions trigger it [auth0-spa-js/src/Auth0Client.ts at f2e566849efa398ca599daf9ebdfbbd62fcb1894 · auth0/auth0-spa-js · GitHub](https://github.com/auth0/auth0-spa-js/blob/f2e566849efa398ca599daf9ebdfbbd62fcb1894/src/Auth0Client.ts#L520)

You can also use the browser dev tools. Go to Application tab \> Session Storage and find a key that starts with `a0.spajs.txs`.

---

<div class="post-metadata">

**Author:** ![nik.baleca](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/nik.baleca/32/68749_2.png) [@nik.baleca](https://community.auth0.com/u/nik.baleca)\
**Post date:** [February 19, 2025, 9:31pm UTC](https://community.auth0.com/t/handleredirectcallback-failing-with-invalid-state-error-empty-response/86333/5 "2025-02-19T21:31:31Z")

</div>

Hi everyone and @SreekarMouli98!

I am sorry about the delayed response to your inquiry!

As @TheMuffinMan has mentioned, this indeed appears to be an issue with the transactionManager which might be deleting the state cookie before the token exchange is completed, resulting in an `Invalid State` error.

I would highly recommend to review this community post regarding the matter where the solution is explained in high details:

> [@Invalid state on reload Auth0 callback url, using auth0-spa-js and Angular 8](https://community.auth0.com/t/invalid-state-on-reload-auth0-callback-url-using-auth0-spa-js-and-angular-8/36469/10):
>
> This issue can be reproduced for different frameworks like React as well. In other words, It isn’t specific to Angular. In summary, before a login transaction starts auth0-spa-js library creates a random state variable and stores it in a cookie. Then, it makes a redirect to Auth0 server with this state parameter. After the user is authenticated Auth0 returns a code along with the same state parameter. Auth-spa-js library checks if the state returned matches with the state stored in the cookie a…

If there are any other questions, feel free to leave a reply!

Kind Regards,  
Nik

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [March 5, 2025, 9:31pm UTC](https://community.auth0.com/t/handleredirectcallback-failing-with-invalid-state-error-empty-response/86333/6 "2025-03-05T21:31:59Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
