All files / src/domain/createSignInRedirectMiddleware index.js

0% Statements 0/33
0% Branches 0/27
0% Functions 0/7
0% Lines 0/17
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66                                                                                                                                   
import log from 'domain/log';
import ApiCall from 'containers/ApiCalls';
import {
  LOAD_USER_ERROR,
  USER_EXPIRED,
  USER_NOT_FOUND,
  USER_SIGNED_OUT,
  SESSION_TERMINATED,
  TRIGGER_USER_CLEAR,
  TRIGGER_SIGNIN_REDIRECT,
  TRIGGER_SIGNOUT_REDIRECT,
} from 'data/SingleSignOn/actions';
import { createUserManager } from 'data/SingleSignOn';
 
const triggerSignInRedirectFlow = (returnUrl) => {
  sessionStorage.lastUrlPath =
      returnUrl || location.pathname + location.search;
  createUserManager().signInRedirectWithValidation();
};
 
const triggerSignOutRedirectFlow = (returnUrl) => {
  sessionStorage.lastUrlPath =
      returnUrl || location.pathname + location.search;
  createUserManager().forceSignOutRedirect();
};
 
export function createSignInRedirectMiddleware() {
  return (store) => (next) => (action) => {
    // bounce any 401s to the login page
    if (ApiCall.Action.isApiAction(action) &&
        ApiCall.Action.isFailure(action) &&
        action.error.status === 401) {
      // going back to the current URL might put us in a loop!
      triggerSignInRedirectFlow(action.returnUrl || '/');
 
    // handle trigger and state change actions
    } else {
      switch (action.type) {
        case LOAD_USER_ERROR:
        case USER_EXPIRED:
        case USER_NOT_FOUND:
        case SESSION_TERMINATED:
          if (location.pathname === '/callback') break;  // still processing the callback
          log.info('createSignInRedirectMiddleware: Caught an invalid user/session action. Redirecting.');
          // intentionally fall through to the next case!
        case TRIGGER_SIGNIN_REDIRECT:  // eslint-disable-line
          triggerSignInRedirectFlow(action.returnUrl);
          // in impersonate mode this prevents an infinite loop
          if (action.type === TRIGGER_SIGNIN_REDIRECT) {
            store.dispatch({ type: TRIGGER_USER_CLEAR });
          }
          break;
        case USER_SIGNED_OUT:
        case TRIGGER_SIGNOUT_REDIRECT:
          triggerSignOutRedirectFlow(action.returnUrl);
          break;
        default: break;
      }
    }
 
    return next(action);
  };
}
 
export default createSignInRedirectMiddleware;