Skip to content

Instantly share code, notes, and snippets.

@thedevdavid
Created February 26, 2018 10:48
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 thedevdavid/874aa092520b1cebd7c346f575f6f53f to your computer and use it in GitHub Desktop.
Save thedevdavid/874aa092520b1cebd7c346f575f6f53f to your computer and use it in GitHub Desktop.
Auth reducer
import types from '../actions/types';
const initialState = {
loggedIn: false,
isFetching: false,
hasError: false,
errorMessage: '',
user: null
};
export default (state = initialState, action) => {
switch (action.type) {
case types.LOGIN_START: {
return {
...state,
isFetching: true
};
}
case types.LOGIN_FINISHED: {
const { user } = action;
return {
...state,
isFetching: false,
loggedIn: true,
user
};
}
case types.LOGIN_ERROR: {
const { error } = action;
return {
...state,
isFetching: false,
loggedIn: false,
hasError: true,
user: null,
errorMessage: error
};
}
case types.LOGOUT_START: {
return {
...state,
isFetching: true
};
}
case types.LOGOUT_FINISHED: {
return {
...initialState
};
}
case types.LOGOUT_ERROR: {
const { error } = action;
return {
...state,
isFetching: false,
loggedIn: true,
hasError: true,
errorMessage: error
};
}
default: {
return state;
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment