text: import {
    USER_LOADING,
    USER_LOADED,
    AUTH_ERROR,
    LOGIN_SUCCESS,
    LOGIN_FAIL,
    LOGOUT_SUCCESS,
    REGISTER_SUCCESS,
    REGISTER_FAIL,
  } from "../constants/auth";
  
  let user = JSON.parse(localStorage.getItem("user"));
  const initialState = user
    ? {
        token: localStorage.getItem("token"),
        isAuthenticated: true,
        isLoading: false,
        user: user,
      }
    : {
        token: localStorage.getItem("token"),
        isAuthenticated: null,
        isLoading: false,
        user: null,
      };
  
  const authReducer = (state = initialState, action) => {
    switch (action.type) {
      case USER_LOADING:
        return {
          ...state,
          isLoading: true,
        };
  
      case USER_LOADED:
        return {
          ...state,
          isAuthenticated: true,
          isLoading: false,
          user: action.payload,
        };
  
      case LOGIN_SUCCESS:
      case REGISTER_SUCCESS:
        localStorage.setItem("token", action.payload.token);
        localStorage.setItem("user", JSON.stringify(action.payload.user));
        return {
          ...state,
          ...action.payload,
          isAuthenticated: true,
          isLoading: false,
        };
  
      case AUTH_ERROR:
      case LOGIN_FAIL:
      case LOGOUT_SUCCESS:
      case REGISTER_FAIL:
        localStorage.removeItem("token");
        localStorage.removeItem("user");
        return {
          ...state,
          token: null,
          user: null,
          isAuthenticated: false,
          isLoading: false,
        };
  
      default:
        return state;
    }
  };
  
  export default authReducer;
  