Files
aso/frontend/dashboard/src/guards/AuthGuard.tsx
Linksehat Staging Server ce024c2bcd merge
2023-05-08 08:50:15 +07:00

39 lines
1.0 KiB
TypeScript
Executable File

import { useState, ReactNode } from 'react';
import { Navigate, useLocation } from 'react-router-dom';
// hooks
import useAuth from '../hooks/useAuth';
// pages
import Login from '../pages/auth/Login';
// components
import LoadingScreen from '../components/LoadingScreen';
// ----------------------------------------------------------------------
type AuthGuardProps = {
children: ReactNode;
};
export default function AuthGuard({ children }: AuthGuardProps) {
const { isAuthenticated, isInitialized } = useAuth();
const { pathname } = useLocation();
const [requestedLocation, setRequestedLocation] = useState<string | null>(null);
if (!isInitialized) {
return <LoadingScreen />;
}
if (!isAuthenticated) {
if (pathname !== requestedLocation) {
setRequestedLocation(pathname);
}
return <Navigate to="/auth/login" replace={true}/>;
}
if (requestedLocation && pathname !== requestedLocation) {
setRequestedLocation(null);
return <Navigate to={requestedLocation} />;
}
return <>{children}</>;
}