import { Component , Injector , OnDestroy , OnInit } from '@angular/core' ; import { CanActivate , Router } from '@angular/router' ; import { accountModuleAnimation } from '@shared/animations/routerTransition' ; import { AppComponentBase } from '@shared/common/app-component-base' ; import { Subscription } from 'rxjs' ; import { timer } from 'rxjs' ; import { LoginService } from './login.service' ; @ Component ( { templateUrl : './validate-two-factor-code.component.html' , styleUrls : [ './validate-two-factor-code.component.less' ] , animations : [ accountModuleAnimation ( ) ] } ) export class ValidateTwoFactorCodeComponent extends AppComponentBase implements CanActivate , OnInit , OnDestroy { code : string ; submitting = false ; remainingSeconds = 90 ; timerSubscription : Subscription ; constructor ( injector , public loginService , private _router ) { super ( injector ) ; } canActivate ( ) { if ( this . loginService . authenticateModel && this . loginService . authenticateResult ) { return true ; } return false ; } ngOnInit ( ) { if ( ! this . canActivate ( ) ) { this . _router . navigate ( [ 'account/login' ] ) ; return ; } const timerSource = timer ( 1000 , 1000 ) ; this . timerSubscription = timerSource . subscribe ( ( ) => { this . remainingSeconds = this . remainingSeconds - 1 ; if ( this . remainingSeconds === 0 ) { this . message . warn ( this . l ( 'TimeoutPleaseTryAgain' ) ) . then ( ( ) => { this . loginService . authenticateModel . twoFactorVerificationCode = null ; this . _router . navigate ( [ 'account/login' ] ) ; } ) ; } } ) ; } ngOnDestroy ( ) { if ( this . timerSubscription ) { this . timerSubscription . unsubscribe ( ) ; this . timerSubscription = null ; } } submit ( ) { this . loginService . authenticateModel . twoFactorVerificationCode = this . code ; this . loginService . authenticate ( ) ; } }