import { useEffect } from 'react';
import { useNavigate } from 'react-router-dom';
import { getUserData } from '../../utils/session.js';

export default function AuthGuard({ children, roles }) {
    const navigate = useNavigate();

    useEffect(() => {
        const user = getUserData();

        if (!user) {
            navigate('/login');
            return;
        }

        const hasNecessaryRole = roles.some(role => user.roles.find(userRole => userRole === role));

        if (!hasNecessaryRole) {
            navigate('/');
            return;
        }
    }, []);

    return (
        <>
            {children}
        </>
    );

}