2022-11-25 20:25:03 +00:00
|
|
|
import type { ReactNode } from 'react';
|
|
|
|
import { Navigate, useLocation } from 'react-router-dom';
|
|
|
|
|
2022-05-28 17:32:35 +00:00
|
|
|
import { useStoreState } from '@/state/hooks';
|
|
|
|
|
2022-11-25 20:25:03 +00:00
|
|
|
function AuthenticatedRoute({ children }: { children?: ReactNode }): JSX.Element {
|
|
|
|
const isAuthenticated = useStoreState(state => !!state.user.data?.uuid);
|
|
|
|
|
|
|
|
const location = useLocation();
|
|
|
|
|
|
|
|
if (isAuthenticated) {
|
|
|
|
return <>{children}</>;
|
|
|
|
}
|
|
|
|
|
|
|
|
return <Navigate to="/auth/login" state={{ from: location.pathname }} />;
|
|
|
|
}
|
2022-05-28 17:32:35 +00:00
|
|
|
|
2022-11-25 20:25:03 +00:00
|
|
|
export default AuthenticatedRoute;
|