text: import * as actionTypes from './actionTypes';
  import axios from '../../axios-orders';
  
  export const authStart = () => {
    return {
      type: actionTypes.AUTH_START,
    };
  };
  
  export const authSuccess = (token, userId) => {
    return {
      type: actionTypes.AUTH_SUCCESS,
      idToken: token,
      userId: userId,
    };
  };
  
  export const authFail = (error) => {
    return {
      type: actionTypes.AUTH_FAIL,
      error: error,
    };
  };
  
  export const logout = () => {
    localStorage.removeItem('token');
    localStorage.removeItem('expirationDate');
    localStorage.removeItem('userId');
    return {
      type: actionTypes.AUTH_LOGOUT,
    };
  };
  
  export const checkAuthTimeout = (expirationTime) => {
    return (dispatch) => {
      setTimeout(() => {
        dispatch(logout());
      }, expirationTime * 1000);
    };
  };
  
  export const auth = (email, password, isSignup, username) => {
    return (dispatch) => {
      dispatch(authStart());
      const authData = {
        email: email,
        password: password,
        returnSecureToken: true,
      };
  
      let url = '/auth/login';
      if (!isSignup) {
        url = '/auth/signup';
      }
      axios
        .post(url, authData)
        .then((response) => {
          const expirationDate = new Date(new Date().getTime() + response.data.expiresIn * 1000);
  
          localStorage.setItem('token', response.data.token);
          localStorage.setItem('expirationDate', expirationDate);
          localStorage.setItem('userId', response.data.userId);
  
          // sessionStorage.setItem("jwtToken", response.data.token);
  
          dispatch(authSuccess(response.data.token, response.data.userId));
          dispatch(checkAuthTimeout(response.data.expiresIn));
        })
        .catch((err) => {
          console.log(err);
          dispatch(authFail(err));
        });
    };
  };
  
  export const setAuthError = (path) => {
    return {
      type: actionTypes.SET_AUTH_ERROR,
    };
  };
  
  export const setAuthRedirectPath = (path) => {
    return {
      type: actionTypes.SET_AUTH_REDIRECT_PATH,
      path: path,
    };
  };
  
  export const authCheckState = () => {
    return (dispatch) => {
      const token = localStorage.getItem('token');
      if (!token) {
        dispatch(logout());
      } else {
        const expirationDate = new Date(localStorage.getItem('expirationDate'));
        if (expirationDate <= new Date()) {
          dispatch(logout());
        } else {
          const userId = localStorage.getItem('userId');
          dispatch(authSuccess(token, userId));
          dispatch(checkAuthTimeout((expirationDate.getTime() - new Date().getTime()) / 1000));
        }
      }
    };
  };
  