import { Component , EventEmitter , Output } from '@angular/core' ; import { Client } from '../../../services/api' ; import { OverlayModalService } from '../../../services/ux/overlay-modal' ; import { ConfirmPasswordModalComponent } from '../../modals/confirm-password/modal.component' ; @ Component ( { moduleId : module . id , selector : 'm-settings--two-factor' , inputs : [ 'object' ] , templateUrl : 'two-factor.component.html' , } ) export class SettingsTwoFactorComponent { telno : string ; number : string ; secret ; waitingForCheck : boolean = false ; sendingSms : boolean = false ; object : any ; inProgress : boolean = false ; error : string = '' ; @ Output ( 'saved' ) saved : EventEmitter < unknown > = new EventEmitter < unknown > ( ) ; @ Output ( 'disabled' ) disabled : EventEmitter < unknown > = new EventEmitter < unknown > ( ) ; constructor ( public client , private overlayModal ) { this . load ( ) ; } load ( ) { this . inProgress = true ; this . client . get ( 'api/v1/twofactor' ) . then ( ( response ) => { if ( response . telno ) this . telno = response . telno ; this . inProgress = false ; } ) ; } numberChange ( number ) { this . number = number ; } returns { Promise < void > } async setup ( ) { try { this . telno = this . number ; const request = this . client . post ( 'api/v1/twofactor/setup' , { tel : this . telno } ) ; this . inProgress = true ; this . sendingSms = true ; this . waitingForCheck = true ; this . error = '' ; const response = await request ; this . sendingSms = false ; this . inProgress = false ; this . secret = response . secret ; } catch ( e ) { this . inProgress = false ; this . sendingSms = false ; this . waitingForCheck = false ; this . telno = null ; if ( e . message === 'voip phones not allowed' ) { this . error = "We don't allow VOIP phones. Please, try again with a different number." ; return ; } this . error = 'An error has occurred, please make sure that the phone number is correct.' ; } } check ( code ) { this . client . post ( 'api/v1/twofactor/check' , { code : code , telno : this . telno , secret : this . secret , } ) . then ( ( response ) => { this . waitingForCheck = false ; this . saved . emit ( true ) ; } ) . catch ( ( response ) => { this . waitingForCheck = false ; this . telno = null ; this . number = null ; this . error = 'The code was incorrect. Please, try again.' ; } ) ; } retry ( ) { this . telno = null ; this . waitingForCheck = false ; } cancel ( ) { const creator = this . overlayModal . create ( ConfirmPasswordModalComponent , { } , { class : 'm-overlay-modal--small' , onComplete : ( { password } ) => { this . client . post ( 'api/v1/twofactor/remove' , { password : password , } ) ; this . telno = null ; this . error = '' ; this . disabled . emit ( true ) ; } , } ) ; creator . present ( ) ; } }