React Redirect to orignal page

Hi everyone,

I’m trying to get react to redirect to the original page after Auth0 sign on. I followed this tutorial (The Complete Guide to React User Authentication with Auth0) but have no luck even though it seemed to be what I needed. This is what I got so far:

const history = useNavigate();

  const onRedirectCallback = (appState) => {
    history.push(appState?.returnTo || window.location.pathname);
  };

  return (
    <Auth0Provider
      domain= { domain }
      clientId= { clientId }
      redirectUri={ window.location.origin }
      onRedirectCallback={ onRedirectCallback }
    >
      { children }
    </Auth0Provider>
  );

I thought the onRedirectCallback would solve the issue but it doesn’t. Also all the possible urls in the page are listed in the allowed Callback Url section.

Thanks for any help!

2 Likes

I think you need to provide the appState when you call the redirect.

if (!isAuthenticated) {
  loginWithRedirect({ appState: { returnTo: window.location.pathname } });
}

(I just started with Auth0 couple days ago, so I could be wrong.)

1 Like

Does anyone know what the technical reason is behind having to redirect via the client? Auth0 doesn’t want us having wildcards in the callback URL and this seems like a workaround to me unless there’s a spec somewhere saying otherwise. A lot of the help handed out here is just along the lines of “Here, do this” which is fine but it’d be nice to know why.

Thanks for sharing this information. It was useful.