-
|
Sorry react newb here, I have a component that redirects the user to whatever protected route they were trying to access after they log in via: and on login. This works by itself. However I'm trying to add the functionality of redirecting the user home if they try to access the login page AFTER they've logged in and have been authenticated via: They seem to be fighting over navigation, as the preservation of previous routes is no longer preserved, as they are all being routed to "/" after logging in since the second navigation is overwriting the first the instant the user logs in and there is a token. How would i construct a case so the second navigation is only run when you try to access the login page while already logged in? These are my routes: |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
|
The protected-route side is recording the right thing. The part that throws it away is the logged-in guard on Use the same export const RestrictedPublicRoute = () => {
const { token } = useAuth();
const location = useLocation();
if (token) {
const from = location.state?.from;
const to = from
? `${from.pathname}${from.search ?? ""}${from.hash ?? ""}`
: "/";
return <Navigate to={to} replace />;
}
return <Outlet />;
};Then have the login submit handler derive the same target and navigate with |
Beta Was this translation helpful? Give feedback.
The protected-route side is recording the right thing. The part that throws it away is the logged-in guard on
/login, because it always redirects to/.Use the same
location.state.fromin that guard, and keep a fallback for direct visits to/login:Then have the login submit handler derive the same target and navigate with
replace. That way a user who was sent to