/** * @Component Name: TimeoutComponent * @Module: App * @Module Functionality : To display the footer information * @Creation Date: October 30,2017 * @Author: sbaireddy * @Version: 1.0 */ import { Component, ViewChild, OnInit, OnDestroy } from '@angular/core'; import { Router } from '@angular/router'; import { OktaAuthService } from '@okta/okta-angular'; import { environment } from './../../../environments/environment'; import { SharedService } from '../../shared/shared.service'; @Component({ moduleId: module.id, selector: 'timeout-cmp', templateUrl: 'timeout.component.html' }) export class TimeoutComponent implements OnInit, OnDestroy { private COMPONENT_NAME: string = 'TimeoutComponent'; @ViewChild('timeoutModal') timeoutModal: any; timedOut: boolean = false; idleState: string; lastPing?: Date = null; SESSION_TIMEOUT: number = 1800; // 30 minutes IDLE_TIMEOUT: number = 120; // 2 minutes idleSecondsTimer: any; idleSecondsCounter: number = 0; constructor(private router: Router, public oktaAuth: OktaAuthService, private sharedService: SharedService) { } /** * This function is used to initialize the directive/component after Angular * first displays the data-bound properties.It is invoked only once when the * directive is instantiated. * @returns void */ ngOnInit(): void { const METHOD_NAME: string = 'ngOnInit()'; if (!this.timedOut) { // Bind the methods to the proper context window.onload = this.resetIdleCounter.bind(this); document.onmousemove = this.resetIdleCounter.bind(this); document.onkeypress = this.resetIdleCounter.bind(this); this.idleSecondsTimer = window.setInterval(() => { this.idleSecondsCounter++; // console.log('after this.idleSecondsCounter:'+ this.idleSecondsCounter); if (this.idleSecondsCounter >= this.SESSION_TIMEOUT) { window.clearInterval(this.idleSecondsTimer); this.idleState = 'Your session has been expired. Please login in again!'; this.timedOut = true; } else if (this.idleSecondsCounter >= (this.SESSION_TIMEOUT - this.IDLE_TIMEOUT)) { this.timeoutModal.show(); this.idleState = 'Your session will expire in ' + (this.IDLE_TIMEOUT)-- + ' seconds, press any key to remain logged in.'; } }, 1000); } } /** * Cleanup just before Angular destroys the directive/component. * Unsubscribe Observables and detach event handlers to avoid memory leaks. * @returns void */ ngOnDestroy(): void { const METHOD_NAME: string = 'ngOnDestroy()'; if (this.idleSecondsTimer) { window.clearInterval(this.idleSecondsTimer); } this.idleSecondsTimer = null; } resetIdleCounter() { const METHOD_NAME: string = 'resetIdleCounter()'; this.timeoutModal.hide(); this.idleSecondsCounter = 0; } clearSession(): void { const METHOD_NAME: string = 'clearSession()'; console.log('Hiding the session timeout model.'); this.sharedService.showSpinner(false); this.sharedService.clearSessionStorage(true); // this.sharedService.clearLocalStorage(); //this.router.navigate(['login']); this.redirectLoginPage();//redirecting to login page } async redirectLoginPage(): Promise { if(environment.oktaEnable) { await this.oktaAuth.logout(); this.oktaAuth.loginRedirect('/login') }else{ this.router.navigate(['login']); } } }