Issues with React Router and LoginwithRedirect

Hey all! I have two login buttons that should direct users to two different routes/components. However, when a user signs in and I provide a specific URL, the redirection always defaults to the root path (‘/’). Is there a way to resolve this issue, or could my current setup be incorrect?


ReactDOM.createRoot(document.getElementById("root")).render(
  <Auth0Provider
    domain={authConfig.domain}
    clientId={authConfig.clientId}
    authorizationParams={authConfig.authorizationParams}
  >
    <QueryClientProvider client={queryClient}>
      <BrowserRouter>
        <StyledEngineProvider injectFirst>
          <ThemeProvider theme={muiTheme}>
            <CssBaseline />
            <App />
          </ThemeProvider>
        </StyledEngineProvider>
        {import.meta.env.MODE === "development" && (
          <ReactQueryDevtools initialIsOpen={false} />
        )}
      </BrowserRouter>
    </QueryClientProvider>
  </Auth0Provider>,
);```


export const authConfig = {
domain: …,
clientId: …,
authorizationParams: {
redirect_uri: window.location.origin,
},
};


const onButtonClick = async (e) => {
e.preventDefault();
await loginWithRedirect({
redirect_uri: ${window.location.origin}/beneficiary-application-page-1,
});
};


  <Button variant="contained" onClick={() => loginWithRedirect()}>
    Login
  </Button>

const App = () => {
return (

{/* Public Route */}
<Route path=“/” element={} />

  {/* Protected Routes */}
  <Route
    path="/dashboard"
    element={
      <ProtectedRoute>
        <Dashboard />
      </ProtectedRoute>
    }
  />
  <Route
    path="/feature-page-1"
    element={
      <ProtectedRoute>
        <FeaturePage1 />
      </ProtectedRoute>
    }
  />
  <Route
    path="/feature-page-2"
    element={
      <ProtectedRoute>
        <FeaturePage2 />
      </ProtectedRoute>
    }
  />
  <Route
    path="/profile-picture-page"
    element={
      <ProtectedRoute>
        <ProfilePicturePage />
      </ProtectedRoute>
    }
  />
  <Route
    path="/edit-page"
    element={
      <ProtectedRoute>
        <EditPage />
      </ProtectedRoute>
    }
  />
  <Route
    path="/application-step-1"
    element={
      <ProtectedRoute>
        <ApplicationStep1 />
      </ProtectedRoute>
    }
  />
  <Route
    path="/criteria-check"
    element={
      <ProtectedRoute>
        <CriteriaCheck />
      </ProtectedRoute>
    }
  />

  {/* Standalone route for dynamic content */}
  <Route path="/:dynamicId" element={<DynamicPage />} />
</Routes>

);
};

export default App;

// ProtectedRoute.jsx
import React from “react”;
import { useAuth0 } from “@auth0/auth0-react”;
import { Navigate } from “react-router-dom”;
import { Box, CircularProgress } from “@mui/material”;

const ProtectedRoute = ({ children }) => {
const { isAuthenticated, isLoading } = useAuth0();

if (isLoading) {
return (
<Box
sx={{
display: “flex”,
justifyContent: “center”,
alignItems: “center”,
height: “100vh”,
}}
>


);
}

return isAuthenticated ? children : ;
};

export default ProtectedRoute;

Hi @solomonsokoya718!

Can you confirm that both the window.location.origin and ${window.location.origin}/beneficiary-application-page-1 are listed in the Allowed Callback URLs of the tenant?

Is the ${window.location.origin}/beneficiary-application-page-1 a protected route? It’s difficult to tell from the code sample. Does that route call loginWithRedirect()?

I found this solution that may prove helpful to you if the answers to the last few questions are “yes”: Angular app non stop refresh after login with code and state query string - #4 by frederik.prijck

Please let me know if I can assist further!

Thanks,

Mary Beth

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