All files / src/containers/SingleSignOn IdleTimeoutHandler.jsx

68.18% Statements 15/22
50% Branches 3/6
66.67% Functions 4/6
68.18% Lines 15/22
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                    5x 2x     3x   3x 3x   3x 3x 3x                           5x 5x       1x   1x     1x               5x       1x          
import { PureComponent } from 'react';
import Timer from './Timer';
import { createUserManager } from 'data/SingleSignOn';
import log from 'domain/log';
import Config from 'domain/Config';
import is from 'is_js';
import PropTypes from 'prop-types';
 
export default class IdleTimeoutHandler extends PureComponent {
  componentWillMount() {
    if (is.not.number(Config.get('autoSignOutTimeout'))) {
      return false;
    }
 
    this.timer = new Timer();
 
    this._signOut = this._signOut.bind(this);
    this._invokeIdleTimer = this._invokeIdleTimer.bind(this);
 
    this._invokeIdleTimer();
    window.document.addEventListener('click', this._invokeIdleTimer);
    window.document.addEventListener('keypress', this._invokeIdleTimer);
  }
 
  componentWillUnmount() {
    if (is.not.number(Config.get('autoSignOutTimeout'))) {
      return false;
    }
 
    this.timer.cancel();
    window.document.removeEventListener('click', this._invokeIdleTimer);
    window.document.removeEventListener('keypress', this._invokeIdleTimer);
  }
 
  _invokeIdleTimer() {
    log.debug('IdleTimeoutHandler - Will start a new timer');
    this.timer.invoke(this._signOut, Config.get('autoSignOutTimeout') * 1000);
  }
 
  _signOut() {
    log.debug('IdleTimeoutHandler - Will sign out!');
    // SignOut should not be with the route hook.
    Iif (this.props.router) {
      this.props.router.setRouteLeaveHook(this._getCurrentRoute(), null);
    }
    createUserManager().forceSignOutRedirect();
  }
 
  _getCurrentRoute() {
    return this.props.routes[this.props.routes.length - 1];
  }
 
  render() {
    return this.props.children;
  }
}
 
IdleTimeoutHandler.propTypes = {
  children: PropTypes.node,
  router: PropTypes.any.isRequired,
  routes: PropTypes.array.isRequired,
};