Skip to content
Discussion options

You must be logged in to vote

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.from in that guard, and keep a fallback for direct visits to /login:

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 replace. That way a user who was sent to

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@vgullberg
Comment options

@cookesan
Comment options

Answer selected by vgullberg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants