auth.js 878 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { createReducer } from '../utils/reducer'
  2. import { REQUEST_OK, REQUEST_KO } from '../constants/ActionTypes'
  3. import { has, isEqual } from 'lodash'
  4. const initialState = {
  5. isAuthenticated: false
  6. }
  7. /**
  8. *
  9. * @param {*} state
  10. * @param {*} action
  11. */
  12. const checkAuthentication = (isAuthenticated, action) => {
  13. if (!has(action.payload, 'status')) {
  14. return isAuthenticated
  15. }
  16. if (isEqual(action.payload.status, 401)) {
  17. return false
  18. }
  19. return isAuthenticated
  20. }
  21. /**
  22. *
  23. */
  24. const authenticationCheckReducer = createReducer(initialState, {
  25. [REQUEST_OK]: checkAuthentication,
  26. [REQUEST_KO]: checkAuthentication
  27. })
  28. /**
  29. *
  30. * @param {*} state
  31. * @param {*} action
  32. */
  33. export const auth = (state = initialState, action) => {
  34. return {
  35. isAuthenticated: authenticationCheckReducer(state.isAuthenticated, action)
  36. }
  37. }