import { createReducer } from '../utils/reducer' import { REQUEST_OK, REQUEST_KO } from '../constants/ActionTypes' import { has, isEqual } from 'lodash' const initialState = { isAuthenticated: false } /** * * @param {*} state * @param {*} action */ const checkAuthentication = (isAuthenticated, action) => { if (!has(action.payload, 'status')) { return isAuthenticated } if (isEqual(action.payload.status, 401)) { return false } return isAuthenticated } /** * */ const authenticationCheckReducer = createReducer(initialState, { [REQUEST_OK]: checkAuthentication, [REQUEST_KO]: checkAuthentication }) /** * * @param {*} state * @param {*} action */ export const auth = (state = initialState, action) => { return { isAuthenticated: authenticationCheckReducer(state.isAuthenticated, action) } }