import { UserInfoResource } from '@cloudbeaver/core-authentication' ; import { Bootstrap , injectable } from '@cloudbeaver/core-di' ; import { CommonDialogService , DialogueStateResult } from '@cloudbeaver/core-dialogs' ; import { ServerConfigResource , SessionExpireService , SessionResource } from '@cloudbeaver/core-root' ; import { getCookies } from '@cloudbeaver/core-utils' ; import { SessionExpireWarningDialog } from '../SessionExpireWarningDialog/SessionExpireWarningDialog' ; const SESSION_COOKIE_NAME = 'cb-session' ; const WARN_IN = 5 * 1000 * 60 ; const POLL_INTERVAL = 1 * 1000 * 60 ; @ injectable ( ) export class SessionExpireWarningDialogService extends Bootstrap { private dialogInternalPromise : Promise < DialogueStateResult | null > | null ; constructor ( private commonDialogService , private sessionExpireService , private serverConfigResource , private sessionResource , private userInfoResource ) { super ( ) ; this . dialogInternalPromise = null ; } register ( ) { this . sessionExpireService . onSessionExpire . addHandler ( this . close . bind ( this ) ) ; } load ( ) { this . startSessionPolling ( ) ; } private startSessionPolling ( ) { const poll = ( ) => { if ( ! this . serverConfigResource . data ?. anonymousAccessEnabled && ! this . userInfoResource . data && ! this . serverConfigResource . configurationMode ) { return ; } const cookies = getCookies ( ) ; const sessionDuration = this . serverConfigResource . data ?. sessionExpireTime ; const sessionExpiredTime = cookies [ SESSION_COOKIE_NAME ] ; if ( ! sessionExpiredTime ) { this . sessionExpireService . sessionExpired ( ) ; return ; } const remainingTime = new Date ( sessionExpiredTime ) . getTime ( ) - Date . now ( ) ; if ( this . sessionExpireService . expired || ! sessionDuration || sessionDuration < WARN_IN ) { this . close ( ) ; return ; } if ( remainingTime < WARN_IN ) { this . open ( ) ; } else { this . close ( ) ; } } ; setInterval ( poll , POLL_INTERVAL ) ; } private async open ( ) { if ( ! this . dialogInternalPromise ) { this . dialogInternalPromise = this . commonDialogService . open ( SessionExpireWarningDialog , null ) ; await this . dialogInternalPromise ; this . dialogInternalPromise = null ; const cookies = getCookies ( ) ; if ( ! this . sessionExpireService . expired ) { if ( cookies [ SESSION_COOKIE_NAME ] ) { await this . sessionResource . refreshSilent ( ) ; } else { this . sessionExpireService . sessionExpired ( ) ; } } } } private close ( ) { if ( this . dialogInternalPromise ) { this . commonDialogService . rejectDialog ( this . dialogInternalPromise ) ; } } }