-
Notifications
You must be signed in to change notification settings - Fork 68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
not private route #967
Comments
@marlena-sliwinska I actually just went through this issue last week. In order to do it, you'll need to disable const AppRouter: React.FC = () => {
const { isLoading, userData, signIn } = useAuth();
const redirectToLogin = () => {
if (!userData && !isLoading) {
void signIn();
}
};
return (
<BrowserRouter>
<Switch>
<Route path="/home"><Redirect to="/" /></Route>
<Route path="/login"><Redirect to="/" /></Route>
<Route path="/user-agreements" component={UserAgreements} />
{userData && (
<UserContainer.Provider>
/* ...private routes here */
</UserContainer.Provider>
)}
<Route render={redirectToLogin} />
</Switch>
</BrowserRouter>
);
}; Essentially, if no userData exists, the user isn't logged in so the "private" routes won't even exist. If the page they attempt to access is one of the non-protected routes (at the top in this example), then it will route accordingly. If none of those match, then it falls through to the final universally matching Route which in turn redirects to the sign in page. |
I am using the newest version of the react-router-dom and as you can see the implementation of routing looks like this: import {
createBrowserRouter,
RouterProvider,
} from "react-router-dom";
import Root, { rootLoader } from "./routes/root";
import Team, { teamLoader } from "./routes/team";
const router = createBrowserRouter([
{
path: "/",
element: <Root />,
loader: rootLoader,
children: [
{
path: "team",
element: <Team />,
loader: teamLoader,
},
],
},
]);
ReactDOM.createRoot(document.getElementById("root")).render(
<RouterProvider router={router} />
); With this type of implementation is not so easy |
I'm having the same problem. |
Closing this as stale. Please open a new issue or let me know you still have this issue in a comment 👍 |
Hi, I've used oidc-react in my project.
As You can see I've wrapped my router with AuthProvider. (I am using react-router and new available API - createBrowserRouter)
<AuthProvider> <AppRouter /> </AuthProvider>
I want to exclude one of the routes from OAuth - make not restricted - available for all users, not only logged. Haw can I do that?
I cannot find it in documentation.
Is there any easy way?
The text was updated successfully, but these errors were encountered: