Uecko_ERP/apps/web/src/routes/public-only-guard.tsx

33 lines
803 B
TypeScript
Raw Normal View History

2026-07-05 11:43:13 +00:00
import { useIdentityAuthSession } from "@erp/identity/client";
import { LoadingOverlay } from "@repo/rdx-ui/components";
2026-06-02 16:40:23 +00:00
import type { ReactNode } from "react";
2026-07-05 11:43:13 +00:00
import { Navigate, useSearchParams } from "react-router-dom";
2026-06-02 16:40:23 +00:00
interface PublicOnlyRouteProps {
children: ReactNode;
}
export const PublicOnlyRouteGuard = ({ children }: PublicOnlyRouteProps) => {
2026-07-06 10:57:33 +00:00
const { getAuthenticatedRedirectPath, isAuthenticated, isLoading } = useIdentityAuthSession();
2026-07-05 11:43:13 +00:00
const [searchParams] = useSearchParams();
const returnTo = searchParams.get("returnTo");
if (isLoading) {
return <LoadingOverlay />;
}
2026-06-02 16:40:23 +00:00
if (isAuthenticated) {
2026-07-06 10:57:33 +00:00
return (
<Navigate
replace
to={getAuthenticatedRedirectPath({
returnTo,
})}
/>
);
2026-06-02 16:40:23 +00:00
}
return <>{children}</>;
};