How do I access the appState I set in loginWithRedirect in a React app?

I have a react app which allows the user to save documents. If the user is not logged in and tries to save it, I want the user to be redirected to a login prompt, and then be sent back to the app, which will then load the unsaved project and show the save dialog. I currently have this code connected to the save button:

const { isAuthenticated, loginWithRedirect } = useAuth0();
async function save() {
  if(!isAuthenticated) {
    loginWithRedirect({
      appState: "insert current document here"
    });
  }
  else {
    // Save the project to a database
  }
}

How can I access the appState once Auth0 redirects the user back to my app?

1 Like

Any answers to this question?

Same question from here.

1 Like

I never figured it out with React but I did figure it out in Next.js: Kobra/callback.ts at cd68aa1d3b9acd7df6ccd46eed791ab9079506ed · kobra-dev/Kobra · GitHub

2 Likes

Thanks for sharing that with the rest of community!

Check out Auth0’s 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:

5 Likes

Thanks for sharing that with the rest of community!

That helped. Thanks!