import { useState, useEffect } from 'react';
import Cookies from 'js-cookie';
import { axiosClient } from '../App.tsx';
function useAuth() {
  const [authorized, setAuthorized] = useState<boolean>(false);

  useEffect(() => {
    async function validateToken() {
      const token = Cookies.get('token');
      if (!token) {
        setAuthorized(false);
        return;
      }

      try {
        await axiosClient.get('/api/auth/validate', { withCredentials: true });

        setAuthorized(true);
      } catch (e) {
        setAuthorized(false);
        console.log('Failed to validate token: ', e);
      }
    }

    validateToken();
  }, []);

  return authorized;
}

export default useAuth;
