import { Component , OnInit } from '@angular/core' ; import { BehaviorSubject , combineLatest , Observable } from 'rxjs' ; import { map , take } from 'rxjs/operators' ; import { AbstractSubscriberComponent } from '../../../../../common/components/abstract-subscriber/abstract-subscriber.component' ; import { FormToastService } from '../../../../../common/services/form-toast.service' ; import { Session } from '../../../../../services/session' ; import { StackableModalService } from '../../../../../services/ux/stackable-modal.service' ; import { SettingsTwoFactorV2Service } from '../two-factor-v2.service' ; import { SettingsTwoFactorCodePopupComponent } from './code-popup/code-popup.component' ; /**
 * Connect app panel - used so the user can scan their QR and verify the code given
 * by their auth app.
 */ @ Component ( { selector : 'm-twoFactor__connectApp' , templateUrl : './connect-app.component.html' , styleUrls : [ './connect-app.component.ng.scss' ] , } ) export class SettingsTwoFactorConnectAppComponent extends AbstractSubscriberComponent implements OnInit { /**
     * User entered code from auth app.
     */ public code$ : BehaviorSubject < string > = new BehaviorSubject < string > ( '' ) ; returns { BehaviorSubject < string > } get secret$ ( ) : BehaviorSubject < string > { return this . service . secret$ ; } returns { Observable < string > } get secretObject$ ( ) : Observable < string > { const username = this . session . getLoggedInUser ( ) . username ; return this . service . secret$ . pipe ( map ( ( code ) => { return `otpauth://totp/Minds:${ username }?secret=${ code }&issuer=Minds` ; } ) ) ; } returns { BehaviorSubject < boolean > } get inProgress$ ( ) : BehaviorSubject < boolean > { return this . service . inProgress$ ; } returns { Observable < boolean > } get disabled$ ( ) : Observable < boolean > { return this . code$ . pipe ( map ( ( code ) => { return code . length !== 6 ; } ) ) ; } constructor ( private service , private session , private stackableModal , private toast ) { super ( ) ; } ngOnInit ( ) { this . service . fetchNewSecret ( ) ; } returns { void } public enableButtonClick ( ) { this . inProgress$ . next ( true ) ; this . subscriptions . push ( combineLatest ( [ this . service . passwordConfirmed$ , this . code$ ] ) . pipe ( take ( 1 ) , map ( ( [ passwordConfirmed , code ] ) => { if ( ! passwordConfirmed ) { this . toast . error ( 'You must go back and provide your password.' ) ; return ; } this . service . submitCode ( code ) ; } ) ) . subscribe ( ) ) ; } returns { void } public backButtonClick ( ) { this . service . activePanel$ . next ( { panel : 'root' } ) ; } param { string } $event returns { void } public codeValueChanged ( $event ) { this . code$ . next ( $event ) ; } returns { Promise < void > } public async onEnterTextCodeClick ( ) { this . subscriptions . push ( this . secret$ . pipe ( take ( 1 ) ) . subscribe ( async ( value ) => { try { await this . stackableModal . present ( SettingsTwoFactorCodePopupComponent , null , { code : value , } ) . toPromise ( ) ; } catch ( e ) { } } ) ) ; } }