# React app redirects to correct url after login but component stays the same

**URL:** <https://community.auth0.com/t/react-app-redirects-to-correct-url-after-login-but-component-stays-the-same/159230>\
**Category:** Get Help\
**Tags:** classic-universal-login-experience, login-experience\
**Created:** [December 8, 2024, 10:10pm UTC](https://community.auth0.com/t/react-app-redirects-to-correct-url-after-login-but-component-stays-the-same/159230 "2024-12-08T22:10:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![grin.mihail](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/grin.mihail/32/61574_2.png) [@grin.mihail](https://community.auth0.com/u/grin.mihail)\
**Post date:** [December 8, 2024, 10:10pm UTC](https://community.auth0.com/t/react-app-redirects-to-correct-url-after-login-but-component-stays-the-same/159230/1 "2024-12-08T22:10:17Z")

</div>

I have a problem with redirecting users to the intended component after login with auth0

When I click on the button to go to the library, the library is a protected component, so it goes to the login page, after login it sends me back to my app, and the route is [http://localhost:5173/library](http://localhost:5173/library) BUT the rendered component is LandingPage

Ask me any questions, as I’m desperate, trying to solve it for too long

my main.js

```auto
import { createRoot } from "react-dom/client";
import "@/styles/index.scss";
import App from "./App.tsx";
import { Auth0Provider } from "@auth0/auth0-react";

createRoot(document.getElementById("root")!).render(
  <Auth0Provider
    domain={import.meta.env.VITE_AUTH0_DOMAIN}
    clientId={import.meta.env.VITE_AUTH0_CLIENT_ID}
    authorizationParams={{
      redirect_uri: window.location.origin + window.location.pathname,
      audience: import.meta.env.VITE_AUTH0_AUDIENCE,
    }}
    cacheLocation="localstorage"
  >
    <App />
  </Auth0Provider>
);

```

ProtectedComponent.ts

```auto
import { withAuthenticationRequired } from "@auth0/auth0-react";

const ProtectedRoute = ({ children }: { children: React.ReactNode }) => {
  const ProtectedComponent = withAuthenticationRequired(() => <>{children}</>, {
    returnTo: window.location.origin + window.location.pathname,
  });

  return <ProtectedComponent />;
};

export default ProtectedRoute;

```

App.tsx

```auto
function App() {
  return (
    <Router>
      <Routes>
        <Route
          path="/quiz-editor"
          element={
            <ProtectedRoute>
              <QuizEditor />
            </ProtectedRoute>
          }
        />
        <Route path="/" element={<LandingPage />} />
        <Route
          path="/library"
          element={
            <ProtectedRoute>
              <Library />
            </ProtectedRoute>
          }
        />
      </Routes>
    </Router>
  );
}

```

---

<div class="post-metadata">

**Author:** ![marybeth.hunter](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/marybeth.hunter/32/61955_2.png) [@marybeth.hunter](https://community.auth0.com/u/marybeth.hunter)\
**Post date:** [December 10, 2024, 8:07pm UTC](https://community.auth0.com/t/react-app-redirects-to-correct-url-after-login-but-component-stays-the-same/159230/3 "2024-12-10T20:07:17Z")

</div>

Hi @grin.mihail,

Welcome to the Auth0 Community!

Can you try using `ProtectedRoute` in this way:

```auto
export default function Routing() {
  return (
    <Routes>
      <Route path="/library" element={<ProtectedRoute component={Library} />} />
    </Routes>
  );
}

```

I set this up on my React application and ran into issues with the following:

```auto
element={
            <ProtectedRoute>
              <Library />
            </ProtectedRoute>
          }

```

Please see here for more information on adding route guards to React: [React Authentication By Example: Using React Router 6](https://developer.auth0.com/resources/guides/spa/react/basic-authentication#add-route-guards-to-react)

I look forward to your reply!

Thanks,

Mary Beth

---

<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:** [December 24, 2024, 8:07pm UTC](https://community.auth0.com/t/react-app-redirects-to-correct-url-after-login-but-component-stays-the-same/159230/4 "2024-12-24T20:07:30Z")

</div>

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