# How to recover React App state after loginWithRedirect

**URL:** <https://community.auth0.com/t/how-to-recover-react-app-state-after-loginwithredirect/99688>\
**Category:** Get Help\
**Tags:** clients, management-api\
**Created:** [February 3, 2023, 9:32pm UTC](https://community.auth0.com/t/how-to-recover-react-app-state-after-loginwithredirect/99688 "2023-02-03T21:32:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tms](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/tms/32/16540_2.png) [@tms](https://community.auth0.com/u/tms)\
**Post date:** [February 3, 2023, 9:32pm UTC](https://community.auth0.com/t/how-to-recover-react-app-state-after-loginwithredirect/99688/1 "2023-02-03T21:32:21Z")

</div>

When a React application calls `loginWithRedirect` with default values for the `options` argument, the state of the application is cleared when the application is called back after the redirect.

The approach described here bundles the state to be recovered into a Javascript literal object and uses `options.appState` so that the inbound redirect contains the desired state.

This is all in the context of React front ends that rely on the `useAuth0` hook.

I invite constructive feedback about issues this approach might create, especially around security. If sensitive information is being exchanged, then it might make sense to encrypt or hash the `appStateJSON` or the values it contains.

The general approach is:

Preparing `loginWithRedirect`:

1. Assemble the parameters to be passed in a Javascript literal object.

2. Use `JSON.stringify` to obtain a JSON-encoded string. I call this “appStateJSON”. I use `encodeURIComponent` to sanitize this for use in a URL.

3. In the “options” of the various redirect Auth0 calls, add a parameter binding with name “appState” and whose value is “appStateJSON” (formed above).

Handling the inbound redirect after login:

1. I modified the current recommended code for `Auth0ProviderWithHistory` as follows:

2. I add the following at or near the top of the React app that receives the redirect:

With this furniture in place, the React app can collect whatever it needs from `transientState` after a redirect.

In this example, ‘principalID’ is a React state variable created as follows:

```auto
const [principalID, setPrincipalID] = useState(null);

```

Here is a `useEffect` hook that reconstructs the `principalID` state on redirect:

```auto
  useEffect(
    () => {
      if ( (principalID === null)
           && (transientState)
           && (transientState.principalID)) {
        setPrincipalID(transientState.principalID)
      }
    },
    [
      principalID,
      transientState,
      setPrincipalID,
    ]
  );

```

So far as I know, this approach is able to pass through arbitrary state (including embedded objects and such). I intentionally chose structures that do not require special magic for `JSON.stringify` and `JSON.parse`.

I’ve been using this code for a few weeks now and it seems to work fine.

Developers who are using Auth0 from React apps will surely need this mechanism.

I strongly encourage the Auth0 team to provide something along these lines in the technical documentation.

---

<div class="post-metadata">

**Author:** ![tms](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/tms/32/16540_2.png) [@tms](https://community.auth0.com/u/tms)\
**Post date:** [February 3, 2023, 9:34pm UTC](https://community.auth0.com/t/how-to-recover-react-app-state-after-loginwithredirect/99688/2 "2023-02-03T21:34:32Z")

</div>

I’m adding this reply just to indicate that this topic is informational, and doesn’t require a “solution”.

---

<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:** [February 17, 2023, 9:35pm UTC](https://community.auth0.com/t/how-to-recover-react-app-state-after-loginwithredirect/99688/3 "2023-02-17T21:35:16Z")

</div>

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