# How to preserve client state after loginWithRedirect?

**URL:** <https://community.auth0.com/t/how-to-preserve-client-state-after-loginwithredirect/95963>\
**Category:** Get Help\
**Tags:** users, management-api\
**Created:** [December 7, 2022, 11:35pm UTC](https://community.auth0.com/t/how-to-preserve-client-state-after-loginwithredirect/95963 "2022-12-07T23:35:39Z")\
**Posts on this page:** 6\
**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:** [December 7, 2022, 11:35pm UTC](https://community.auth0.com/t/how-to-preserve-client-state-after-loginwithredirect/95963/1 "2022-12-07T23:35:39Z")

</div>

I’ve been thrashing at this for weeks now with no progress.

I have a client-side React SPA. It is responsible for signing in a user. It calls `loginWithRedirect`. All is fine. EXCEPT …

After a successful login, the client-side SPA has to itself redirect back to the app that called it. The Auth0 documentation is confusing about how to accomplish that.

The client-side React App passes a reference to itself (`https://localhost:3103/loginPersona`) as the value of `redirectUri` in the options.

When that redirectUri is called, it is called with no other data in the query string or body.

When I add a `state` parameter to the options, I see no evidence that Auth0 does anything with it at all.

According to [one Auth0 document](https://auth0.com/docs/secure/attack-protection/state-parameters?_gl=1*17fb1qi*rollup_ga*MjAxODQ2NDUyLjE2NjM4OTA2MTE.*rollup_ga_F1G3E656YZ*MTY3MDQ1Mjc2Mi43Mi4xLjE2NzA0NTI4MTMuOS4wLjA.&_ga=2.96212444.1719022123.1670422955-201846452.1663890611#redirect-users):

> After the request is sent, the user is redirected back to the application by Auth0. The `state` value will be included in this redirect. Note that depending on the type of connection used, this value might be in the body of the request or in the query string.

When I set a breakpoint in the callback and examine the value of `window.location`, there is no  
evidence that the value of `state` that I passed is present in the resulting callback at all.

I fear I am missing something truly basic here – I’ve been at this task for weeks and I’m making no progress. Is there SOME sort of community that can offer some guidance?

Is it really this hard to sign in users?

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [December 12, 2022, 11:26pm UTC](https://community.auth0.com/t/how-to-preserve-client-state-after-loginwithredirect/95963/3 "2022-12-12T23:26:26Z")

</div>

Hey there @tms !

> [@tms](#):
>
> I fear I am missing something truly basic here – I’ve been at this task for weeks and I’m making no progress. Is there SOME sort of community that can offer some guidance?

Have you taken a look at the [appState](https://auth0.github.io/auth0-react/interfaces/RedirectLoginOptions.html#appState) property?

> [@How do I access the appState I set in loginWithRedirect in a React app?](https://community.auth0.com/t/how-do-i-access-the-appstate-i-set-in-loginwithredirect-in-a-react-app/53162/6):
>
> Check out Auth0’s [Complete Guide to React User Authentication](https://auth0.com/blog/complete-guide-to-react-user-authentication/). In src/auth/auth0-provider-with-history.js, there’s an arrow function named onRedirectCallback, which accesses appState. Accessing appState in this callback function worked as I expected. Hope this breadcrumb helps someone. I’ll add a more complete answer if I get the chance later. wink

Let us know if that works for your use case!

---

<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:** [December 15, 2022, 5:44pm UTC](https://community.auth0.com/t/how-to-preserve-client-state-after-loginwithredirect/95963/4 "2022-12-15T17:44:36Z")

</div>

Hi there! Sadly, I missed this reply earlier – I thought I was subscribed to email notifications on this topic. I’ve checked my inbound emails (I save everything), and I’ve received no notifications about this topic even though the UI says that I’m “watching” it. My most recent notification email from ‘[community.auth0.com](http://community.auth0.com)’ is a notification for a different thread (for a reply that’s also from you). Perhaps there’s an issue with the forum itself?

The top of the document you cite has a prominent note that it has been deprecated. It offers a link to [“React Authentication By Example” developer guide](https://developer.auth0.com/resources/guides/spa/react/basic-authentication?_ga=2.144482229.1218109937.1671121655-201846452.1663890611&_gl=1*crr3p5*rollup_ga*MjAxODQ2NDUyLjE2NjM4OTA2MTE.*rollup_ga_F1G3E656YZ*MTY3MTEyMTY1NS43Ny4xLjE2NzExMjUyMDQuNDYuMC4w).

I’m already familiar with both versions of that document – I see minimal mention of the `appState` parameter, and I haven’t yet found it in the nest of the Auth0 API documentation.

I’ll investigate this further by adding a value for `appState`. I will appreciate a URL to an API document that describes this parameter. It isn’t obvious to me what the Auth0 code expects as its value – perhaps a URL-encoded query string?

I appreciate your attention. I find myself wondering if there’s some way I might be able to contribute to an effort to improve the Auth0 documentation. This seems like solid code – I’m just struggling mightily to get my use-case actually running smoothly. I suspect the needed behavior has been present all along – it’s just taking me weeks to find it.

---

<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:** [December 15, 2022, 11:00pm UTC](https://community.auth0.com/t/how-to-preserve-client-state-after-loginwithredirect/95963/5 "2022-12-15T23:00:43Z")

</div>

From a breakpoint set in the `ProviderWithHistory.onRedirectCallback` method,  
I can see that the `appState` value I added to the `options` in `loginWithRedirect` is available in the `ProviderWithHistory.onRedirectCallback` method (it’s passed as the argument of that method).

I still don’t understand how to make the value(s) of `appState` available to the application.

For example, after a successful login I see the following value for `appState`:

```auto
"continuation=https://localhost:3003/browser&someKey=someValue"

```

This is progress.

This value has two keys: `continuation` and `someKey`.

I would like these two keys to be passed as props to the children of my container. How do I accomplish this?

---

<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:15pm UTC](https://community.auth0.com/t/how-to-preserve-client-state-after-loginwithredirect/95963/6 "2023-02-03T21:15:22Z")

</div>

I have finally found an approach that seems to work. I’m describing it here to preserve the context of the original topic. I’m contemplating the creation of a new topic that describes the more general problem it solves (passing parameters in the redirect URL).

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’m happy to create a new topic with just this response if the community thinks it will be helpful.

I strongly encourage the Auth0 team to provide something along these lines in the technical documentation. It took me months to finally hack together something that works.

---

<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:16pm UTC](https://community.auth0.com/t/how-to-preserve-client-state-after-loginwithredirect/95963/7 "2023-02-17T21:16:11Z")

</div>

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