Last active
February 24, 2017 20:04
-
-
Save nesbtesh/29d6451f4dde2e3f03d47d205dafd2e9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {createReducer} from '../utils'; | |
import {LOGIN_USER_REQUEST, LOGIN_USER_SUCCESS, LOGIN_USER_FAILURE, LOGOUT_USER} from '../constants'; | |
import {pushState} from 'redux-router'; | |
import jwtDecode from 'jwt-decode'; | |
const initialState = { | |
token: null, | |
userName: null, | |
isAuthenticated: false, | |
isAuthenticating: false, | |
statusText: null | |
}; | |
export default createReducer(initialState, { | |
[LOGIN_USER_REQUEST]: (state, payload) => { | |
return Object.assign({}, state, { | |
'isAuthenticating': true, | |
'statusText': null | |
}); | |
}, | |
[LOGIN_USER_SUCCESS]: (state, payload) => { | |
return Object.assign({}, state, { | |
'isAuthenticating': false, | |
'isAuthenticated': true, | |
'token': payload.token, | |
'userName': jwtDecode(payload.token).userName, | |
'statusText': 'You have been successfully logged in.' | |
}); | |
}, | |
[LOGIN_USER_FAILURE]: (state, payload) => { | |
return Object.assign({}, state, { | |
'isAuthenticating': false, | |
'isAuthenticated': false, | |
'token': null, | |
'userName': null, | |
'statusText': `Authentication Error: ${payload.status} ${payload.statusText}` | |
}); | |
}, | |
[LOGOUT_USER]: (state, payload) => { | |
return Object.assign({}, state, { | |
'isAuthenticated': false, | |
'token': null, | |
'userName': null, | |
'statusText': 'You have been successfully logged out.' | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment