import { useEffect } from 'react';
import { useLocation, useNavigate } from 'react-router-dom';

// project import
import useAuth from '@/hooks/useAuth';


// ==============================|| AUTH GUARD ||============================== //

const AuthGuard = ({ children }:BasicComponentProps) => {
    const { isLoggedIn,isInitialized } = useAuth();
    const navigate = useNavigate();
    const location = useLocation();

    useEffect(() => {
        if (isInitialized && !isLoggedIn && location.pathname !== '/user') {
            console.log(location)
            navigate(`/user?from=${location.pathname}`, {
                state: {
                    from: location.pathname
                },
                replace: true
            });
        }
    }, [isLoggedIn, navigate, location]);

    return children;
};

export default AuthGuard;
