text: import {
    REGISTER_SUCCESS,
    USER_LOADED,
    AUTH_ERROR,
    LOGIN_SUCCESS,
    LOGOUT,
    ACCOUNT_DELETED,
  } from '../actions/types'
  
  const initialState = {
    token: localStorage.getItem('token'),
    isAuth: null,
    loading: true,
    user: null,
  }
  
  function authReducer(state = initialState, action) {
    const { type, payload } = action
    switch (type) {
      case USER_LOADED:
        return {
          ...state,
          isAuth: true,
          loading: false,
          user: payload,
        }
      case REGISTER_SUCCESS:
      case LOGIN_SUCCESS:
        localStorage.setItem('token', payload.token)
        return {
          ...state,
          ...payload,
          isAuth: true,
          loading: false,
        }
      case AUTH_ERROR:
      case LOGOUT:
      case ACCOUNT_DELETED:
        localStorage.removeItem('token')
        return {
          ...state,
          token: null,
          isAuth: false,
          loading: false,
          user: null,
        }
      default:
        return state
    }
  }
  
  export default authReducer
  