cbp-lib
Version:
Libraries for cbp
57 lines (49 loc) • 2.04 kB
JavaScript
'use strict'
import {SessionIFrame} from './SessionIFrame'
import {Log} from '../_helpers/Log'
import { Validation } from '../_helpers/custom-error';
export class SessionMonitor {
constructor(authenticationOidc, SessionIFrameCtor = SessionIFrame) {
if (!authenticationOidc) {
Log.error("SessionMonitor: No authenticationoidc passed to SessionMonitor");
throw new Error("AuthenticationOidc Required");
}
this._authenticationOidc = authenticationOidc
this._metaData = this._authenticationOidc.options
this._SessionIFrameCtor = SessionIFrameCtor
this._authenticationOidc.querySessionState()
.then(session_state => {
if (!Validation.isEmpty(session_state)) {
this._start(session_state)
}
})
.catch(error => {
Log.error("SessionMonitor: " + error)
})
}
_start(session_state) {
if (session_state) {
if (!this._sessionIFrame) {
if (this._metaData.check_session_iframe &&
!Validation.isEmpty(this._metaData.check_session_iframe)) {
Log.debug("SessionMonitor.start: Initializing sessionIframe...")
this._sessionIFrame = new this._SessionIFrameCtor(this.callback.bind(this), this._metaData)
this._sessionIFrame.load().then(() => {
this._sessionIFrame.start(session_state)
})
}
else {
Log.warn('SessionMonitor: No check_session_iframe define in the options')
}
}
else {
Log.debug("SessionMonitor.start SessionIFrame is intantiated.")
this._sessionIFrame.start(session_state)
}
}
}
callback(data) {
Log.debug("SessionMonitor.callback: Callback function called")
this._authenticationOidc.event._signOutEvent(data)
}
}