import React, { useEffect, useState } from 'react';
import { jwtDecode } from 'jwt-decode';


const useAuth = () => {
  const [isAuthenticated, setIsAuthenticated] = useState(false);
  const [userRole, setUserRole] = useState(null);

  useEffect(() => {
    const token = localStorage.getItem('token');
    if (token) {
      try {
        const decoded = jwtDecode(token);
        setIsAuthenticated(true);
        setUserRole(decoded.user.role);
        // console.log(decoded.user.role)
      } catch (error) {
        console.error('Failed to decode token:', error);
        setIsAuthenticated(false);
        setUserRole(null);
      }
    }
  }, []);


  return { isAuthenticated, userRole };
};

export default useAuth;
