client/src/reducers/authReducer.js
changeset 62 b2514a9bcd49
parent 53 d8588379529e
child 67 9206af01f5e5
equal deleted inserted replaced
61:7586b4a11c32 62:b2514a9bcd49
       
     1 import Immutable from 'immutable';
     1 import * as types from '../constants/actionTypes';
     2 import * as types from '../constants/actionTypes';
     2 
     3 
     3 export const isAuthenticated = (state = false, action) => {
     4 export const isAuthenticated = (state = false, action) => {
     4   switch (action.type) {
     5   switch (action.type) {
       
     6     case types.AUTH_LOGOUT:
       
     7       return false;
       
     8     case types.AUTH_LOGIN_SUCCESS:
       
     9       return true;
     5     default:
    10     default:
     6       return state;
    11       return state;
     7   }
    12   }
     8 }
    13 }
     9 
    14 
    10 export const currentUser = (state = null, action) => {
    15 export const currentUser = (state = null, action) => {
    11   switch (action.type) {
    16   switch (action.type) {
    12     case types.AUTH_LOGOUT:
    17     case types.AUTH_LOGOUT:
    13       localStorage.removeItem('currentUser');
       
    14       return null;
    18       return null;
    15     case types.AUTH_LOGIN_SUCCESS:
    19     case types.AUTH_LOGIN_SUCCESS:
    16       return action.user;
    20       return Immutable.Map(action.user);
    17     case types.USER_UPDATE_SETTINGS:
    21     case types.USER_UPDATE_SETTINGS:
    18       state.first_name = action.firstname;
    22       return state.merge({
    19       state.last_name = action.lastname;
    23         first_name: action.firstname,
    20       localStorage.setItem('currentUser', JSON.stringify(state));
    24         last_name: action.lastname
    21       return state;
    25       });
    22     default:
    26     default:
    23       return state;
    27       return state;
    24   }
    28   }
    25 }
    29 }
    26 
    30 
    27 export const token = (state = null, action) => {
    31 export const token = (state = null, action) => {
    28   switch (action.type) {
    32   switch (action.type) {
    29     case types.AUTH_LOGOUT:
    33     case types.AUTH_LOGOUT:
    30       localStorage.removeItem('token');
       
    31       return null;
    34       return null;
    32     case types.AUTH_STORE_TOKEN:
    35     case types.AUTH_STORE_TOKEN:
    33       return action.token;
    36       return action.token;
    34     default:
    37     default:
    35       return state;
    38       return state;
    36   }
    39   }
    37 }
    40 }
    38 
    41 
    39 // TODO Use Immutable.Map
    42 const loginState = Immutable.Map({
    40 const loginState = {
       
    41   loading: false,
    43   loading: false,
    42   success: false,
    44   success: false,
    43   error: false,
    45   error: false,
    44 }
    46 });
    45 
    47 
    46 export const login = (state = loginState, action) => {
    48 export const login = (state = loginState, action) => {
    47   switch (action.type) {
    49   switch (action.type) {
    48     case types.AUTH_LOGIN_REQUEST:
    50     case types.AUTH_LOGIN_REQUEST:
    49       return {
    51       return Immutable.Map({
    50         loading: true,
    52         loading: true,
    51         success: false,
    53         success: false,
    52         error: false,
    54         error: false,
    53       }
    55       })
    54     case types.AUTH_LOGIN_SUCCESS:
    56     case types.AUTH_LOGIN_SUCCESS:
    55     case types.AUTH_LOGIN_ERROR:
    57     case types.AUTH_LOGIN_ERROR:
    56       return {
    58       return Immutable.Map({
    57         loading: false,
    59         loading: false,
    58         success: action.type === types.AUTH_LOGIN_SUCCESS,
    60         success: action.type === types.AUTH_LOGIN_SUCCESS,
    59         error: action.type === types.AUTH_LOGIN_ERROR,
    61         error: action.type === types.AUTH_LOGIN_ERROR,
    60       }
    62       })
    61     default:
    63     default:
    62       return state
    64       return state
    63   }
    65   }
    64 }
    66 }