client/src/reducers/authReducer.js
changeset 44 3b20e2b584fe
parent 12 48ddaa42b810
child 47 64428c7ebc19
equal deleted inserted replaced
43:3c9d3c8f41d1 44:3b20e2b584fe
       
     1 import * as types from '../constants/actionTypes';
       
     2 
     1 export const isAuthenticated = (state = false, action) => {
     3 export const isAuthenticated = (state = false, action) => {
     2   switch (action.type) {
     4   switch (action.type) {
     3     default:
     5     default:
     4       return state;
     6       return state;
     5   }
     7   }
     6 };
     8 }
       
     9 
       
    10 export const currentUser = (state = null, action) => {
       
    11   switch (action.type) {
       
    12     case types.AUTH_LOGIN_SUCCESS:
       
    13       return action.user;
       
    14     default:
       
    15       return state;
       
    16   }
       
    17 }
       
    18 
       
    19 export const token = (state = null, action) => {
       
    20   switch (action.type) {
       
    21     case types.AUTH_LOGIN_SUCCESS:
       
    22       console.log('token', action.token)
       
    23       return action.token;
       
    24     default:
       
    25       return state;
       
    26   }
       
    27 }
       
    28 
       
    29 // TODO Use Immutable.Map
       
    30 const loginState = {
       
    31   loading: false,
       
    32   success: false,
       
    33   error: false,
       
    34 }
       
    35 
       
    36 export const login = (state = loginState, action) => {
       
    37   switch (action.type) {
       
    38     case types.AUTH_LOGIN_REQUEST:
       
    39       return {
       
    40         loading: true,
       
    41         success: false,
       
    42         error: false,
       
    43       }
       
    44     case types.AUTH_LOGIN_SUCCESS:
       
    45     case types.AUTH_LOGIN_ERROR:
       
    46       return {
       
    47         loading: false,
       
    48         success: action.type === types.AUTH_LOGIN_SUCCESS,
       
    49         error: action.type === types.AUTH_LOGIN_ERROR,
       
    50       }
       
    51     default:
       
    52       return state
       
    53   }
       
    54 }