Files
AFFiNE-Mirror/apps/ligo-virgo/src/pages/RoutePrivate.tsx
T
2022-08-12 16:57:14 +08:00

38 lines
961 B
TypeScript

import { Navigate, useLocation } from 'react-router-dom';
import { PageLoading, Error } from '@toeverything/components/account';
import { useUserAndSpaces } from '@toeverything/datasource/state';
export type RoutePrivateProps = {
children: JSX.Element;
unauthorizedRedirectTo?: string;
};
/**
* A routing component that cannot be accessed without logging in, and can only be accessed after logging in.
*/
export function RoutePrivate({
children,
unauthorizedRedirectTo = '/login',
}: RoutePrivateProps) {
const { pathname } = useLocation();
const { user, loading } = useUserAndSpaces();
if (user == null && loading) {
return <PageLoading />;
}
if (!user || !pathname.startsWith(`/${user.id}`)) {
return (
<Navigate
to={unauthorizedRedirectTo}
state={{ from: pathname }}
replace={true}
/>
);
}
return children;
}