Skip to content

Instantly share code, notes, and snippets.

@leggomuhgreggo
Created April 12, 2018 20:48
Show Gist options
  • Save leggomuhgreggo/07ee1136e0158893357b2c7a09fa25b8 to your computer and use it in GitHub Desktop.
Save leggomuhgreggo/07ee1136e0158893357b2c7a09fa25b8 to your computer and use it in GitHub Desktop.
Second version of check section
...
const checkSessionThunk = async (dispatch, getState, actionObj) => {
const { user } = getState();
const sessionIsValid = () =>
userHasExistingSession(user) && userSessionHasExpired(user);
sessionIsValid()
? handleValidSession(dispatch, getState, actionObj)
: handleInvalidSession(dispatch, getState, actionObj);
};
export default checkSessionThunk;
const handleValidSession = (dispatch, getState, actionObj) => {
const { action: { type: currentRouteType } } = actionObj;
const { redirectURL } = getState();
if (hasValidRedirectURL(redirectURL)) {
return dispatch(goToRedirectURL());
}
if (isRouteTypeLogin(currentRouteType)) {
return dispatch(goToResources());
}
return true;
};
const handleInvalidSession = (dispatch, getState, actionObj) => {
const { location: { routesMap } } = getState();
const { action: { type: currentRouteType } } = actionObj;
if (isPrivilegedRoute({ routesMap, currentRouteType })) {
return dispatch(goToLogout());
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment