Skip to content

Instantly share code, notes, and snippets.

@nesbtesh
Last active February 24, 2017 20:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nesbtesh/29d6451f4dde2e3f03d47d205dafd2e9 to your computer and use it in GitHub Desktop.
Save nesbtesh/29d6451f4dde2e3f03d47d205dafd2e9 to your computer and use it in GitHub Desktop.
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