allow-logged-in-user-only-guard.tsx•1.12 kB
import { Navigate, useLocation } from 'react-router-dom';
import { SocketProvider } from '@/components/socket-provider';
import { useTelemetry } from '@/components/telemetry-provider';
import { flagsHooks } from '@/hooks/flags-hooks';
import { platformHooks } from '@/hooks/platform-hooks';
import { projectHooks } from '@/hooks/project-hooks';
import { authenticationSession } from '../../lib/authentication-session';
type AllowOnlyLoggedInUserOnlyGuardProps = {
children: React.ReactNode;
};
export const AllowOnlyLoggedInUserOnlyGuard = ({
children,
}: AllowOnlyLoggedInUserOnlyGuardProps) => {
const { reset } = useTelemetry();
const location = useLocation();
if (!authenticationSession.isLoggedIn()) {
authenticationSession.logOut();
reset();
const searchParams = new URLSearchParams();
searchParams.set('from', location.pathname + location.search);
return <Navigate to={`/sign-in?${searchParams.toString()}`} replace />;
}
platformHooks.useCurrentPlatform();
flagsHooks.useFlags();
projectHooks.useCurrentProject();
return <SocketProvider>{children}</SocketProvider>;
};