text: import React, { useState, useEffect } from 'react';
  
  let logoutTimer;
  
  const AuthContext = React.createContext({
      token: '',
      isLoggedIn: false,
      login: (token) => {},
      logout: () => {}
  });
  
  const calculateRemainingDuration = (expirationTime) => {
      const currentTime = new Date().getTime();
      const adjExpirationTime = new Date(expirationTime).getTime();
      const remainingTime = adjExpirationTime - currentTime;
      return remainingTime;
  };
  
  const retrieveStoredToken = () => {
      const storedToken = localStorage.getItem('token');
      const storedExpirationDate = localStorage.getItem('expirationTime');
      const reminingTime = calculateRemainingDuration(storedExpirationDate);
      if (reminingTime <= 3600) {
          localStorage.removeItem('token');
          localStorage.removeItem('expirationTime');
          return null;
      }
  
      return { token: storedToken, duration: reminingTime };
  };
  
  export const AuthContextProvider = (props) => {
      const tokenData = retrieveStoredToken();
      let initialToken;
      if (tokenData) {
          initialToken = tokenData.token;
      }
      const [token, setToken] = useState(initialToken);
  
      const useIsLoggedIn = !!token;
  
      const logoutHandler = () => {
          setToken(null);
          localStorage.removeItem('token');
          localStorage.removeItem('expirationTime');
          if (logoutTimer) {
              clearTimeout(logoutTimer);
          }
      };
  
      const loginHandler = (token, expirationTime) => {
          setToken(token);
          localStorage.setItem('token', token);
          localStorage.setItem('expirationTime', expirationTime);
          const remainingTime = calculateRemainingDuration(
              expirationTime.toISOString()
          );
          logoutTimer = setTimeout(logoutHandler, remainingTime);
      };
  
      useEffect(() => {
          if (tokenData) {
              logoutTimer = setTimeout(logoutHandler, tokenData.duration);
          }
      }, [tokenData]);
  
      const contextValue = {
          token: token,
          isLoggedIn: useIsLoggedIn,
          login: loginHandler,
          logout: logoutHandler
      };
  
      return (
          <AuthContext.Provider value={contextValue}>
              {props.children}
          </AuthContext.Provider>
      );
  };
  
  export default AuthContext;
  