memos/web/src/router/guards.tsx
boojack aa5cb455e9 refactor(web): use / as the home route instead of /home
Home now lives at `/` directly. Unauthenticated visitors are sent to
`/explore` by the landing gate; old `/home` URLs redirect to `/` for
bookmark compatibility.
2026-05-12 23:17:46 +08:00

67 lines
2.1 KiB
TypeScript

import { Navigate, Outlet, useLocation, useSearchParams } from "react-router-dom";
import useCurrentUser from "@/hooks/useCurrentUser";
import { AUTH_REDIRECT_PARAM, buildAuthRoute, getSafeRedirectPath } from "@/utils/auth-redirect";
import { ROUTES } from "./routes";
/**
* Index-route gate mounted at `/`. Authenticated visitors fall through to the
* nested Home page; unauthenticated visitors are redirected to `/explore`,
* preserving the original query string and hash so bookmarks like `/?filter=foo`
* keep working.
*/
export const LandingRoute = () => {
const currentUser = useCurrentUser();
const location = useLocation();
if (!currentUser) {
return (
<Navigate
to={{
pathname: ROUTES.EXPLORE,
search: location.search,
hash: location.hash,
}}
replace
/>
);
}
return <Outlet />;
};
/**
* Guard for routes that require an authenticated user. Unauthenticated visitors
* are redirected to `/auth` with the original location preserved as the `redirect`
* query parameter, so they return to the intended page after signing in.
*/
export const RequireAuthRoute = () => {
const currentUser = useCurrentUser();
const location = useLocation();
if (!currentUser) {
const redirect = `${location.pathname}${location.search}${location.hash}`;
return <Navigate to={buildAuthRoute({ redirect })} replace />;
}
return <Outlet />;
};
/**
* Guard for guest-only routes (sign-in and sign-up). Already-authenticated users
* are redirected to the requested `redirect` target (when safe) or to `/`.
*
* The OAuth callback route (`/auth/callback`) intentionally opts out of this guard:
* an authenticated session in another tab must not prevent the callback from
* consuming its one-time OAuth state and completing the in-flight sign-in.
*/
export const RequireGuestRoute = () => {
const currentUser = useCurrentUser();
const [searchParams] = useSearchParams();
if (currentUser) {
const redirectTarget = getSafeRedirectPath(searchParams.get(AUTH_REDIRECT_PARAM));
return <Navigate to={redirectTarget || ROUTES.HOME} replace />;
}
return <Outlet />;
};