import { useNavigate } from "react-router-dom";
import { useEffect } from "react";
import PropTypes from "prop-types";

function ProtectedRoute({ children }) {
    const navigate = useNavigate();
    const token = localStorage.getItem("token");
    console.log("token", token)

    useEffect(() => {
        if (!token) {
            navigate("/AdminPLogin"); // Redirect to homepage if token is missing
        }
    }, [token, navigate]);

    return token ? children : null;
}

ProtectedRoute.propTypes = {
    children: PropTypes.node.isRequired, // Validate the presence of children
};

export default ProtectedRoute;