AllenYuan

auth - commonreducer

Apr 24th, 2020
440
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const defaultState = {
  2.   appName: 'Conduit',
  3.   token: null
  4. };
  5.  
  6. export default (state = defaultState, action) => {
  7.   switch (action.type) {
  8.     // APP_LOAD => set token, user, loaded state
  9.     case 'APP_LOAD':
  10.       return {
  11.         ...state,
  12.         token: action.token || null,
  13.         appLoaded: true,
  14.         currentUser: action.payload ? action.payload.user : null,
  15.       };
  16.     // REDIRECT ->< remove redirectTO from state
  17.     case 'REDIRECT':
  18.       return {...state, redirectTo: null};
  19.     // LOGIN => redirect to home, put token and user into state
  20.     case 'LOGIN':
  21.       return {
  22.         ...state,
  23.         redirectTo: action.error ? null : '/',
  24.         token: action.error ? null : action.payload.user.token,
  25.         currentUser: action.error ? null : action.payload.user,
  26.       };
  27.     default:
  28.       return state;
  29.   }
  30. };
Advertisement
Add Comment
Please, Sign In to add comment