All files / src/containers/SingleSignOn OidcProvider.js

0% Statements 0/67
0% Branches 0/33
0% Functions 0/18
0% Lines 0/36
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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88                                                                                                                                                                               
import React, { Component } from 'react';
import PropTypes from 'prop-types';
 
import {
  userExpired,
  userFound,
  silentRenewError,
  sessionTerminated,
  userExpiring,
  userSignedOut,
} from 'data/SingleSignOn/actions';
 
class OidcProvider extends Component {
  constructor(props) {
    super(props);
    this.userManager = props.userManager;
    this._onUserLoaded = this._onUserLoaded.bind(this);
    this._onSilentRenewError = this._onSilentRenewError.bind(this);
    this._onAccessTokenExpired = this._onAccessTokenExpired.bind(this);
    this._onUserUnloaded = this._onUserUnloaded.bind(this);
    this._onAccessTokenExpiring = this._onAccessTokenExpiring.bind(this);
    this._onAccessTokenExpired = this._onAccessTokenExpired.bind(this);
    this._onUserSignedOut = this._onUserSignedOut.bind(this);
  }
 
  componentWillMount() {
    // register the event callbacks
    this.userManager.events.addUserLoaded(this._onUserLoaded);
    this.userManager.events.addSilentRenewError(this._onSilentRenewError);
    this.userManager.events.addAccessTokenExpired(this._onAccessTokenExpired);
    this.userManager.events.addAccessTokenExpiring(this._onAccessTokenExpiring);
    this.userManager.events.addUserUnloaded(this._onUserUnloaded);
    this.userManager.events.addUserSignedOut(this._onUserSignedOut);
  }
 
  componentWillUnmount() {
    // unregister the event callbacks
    this.userManager.events.removeUserLoaded(this._onUserLoaded);
    this.userManager.events.removeSilentRenewError(this._onSilentRenewError);
    this.userManager.events.removeAccessTokenExpired(this._onAccessTokenExpired);
    this.userManager.events.removeAccessTokenExpiring(this._onAccessTokenExpiring);
    this.userManager.events.removeUserUnloaded(this._onUserUnloaded);
    this.userManager.events.removeUserSignedOut(this._onUserSignedOut);
  }
 
  // event callback when the user has been loaded (on silent renew or redirect)
  _onUserLoaded(user) {
    this.props.store.dispatch(userFound(user));
  }
 
  // event callback when silent renew errored
  _onSilentRenewError(error) {
    this.props.store.dispatch(silentRenewError(error));
  }
 
  // event callback when the access token expired
  _onAccessTokenExpired() {
    this.props.store.dispatch(userExpired());
  }
 
  // event callback when the user is logged out
  _onUserUnloaded() {
    this.props.store.dispatch(sessionTerminated());
  }
 
  // event callback when the user is expiring
  _onAccessTokenExpiring() {
    this.props.store.dispatch(userExpiring());
  }
 
  // event callback when the user is signed out
  _onUserSignedOut() {
    this.props.store.dispatch(userSignedOut());
  }
 
  render() {
    return React.Children.only(this.props.children);
  }
}
 
OidcProvider.propTypes = {
  userManager: PropTypes.object.isRequired,
  store: PropTypes.object.isRequired,
  children: PropTypes.node,
};
 
export default OidcProvider;