import { Directive , Input } from "@angular/core" ; import { EnvironmentService } from "jslib-common/abstractions/environment.service" ; import { I18nService } from "jslib-common/abstractions/i18n.service" ; import { PlatformUtilsService } from "jslib-common/abstractions/platformUtils.service" ; import { CaptchaIFrame } from "jslib-common/misc/captcha_iframe" ; import { Utils } from "jslib-common/misc/utils" ; @ Directive ( ) export abstract class CaptchaProtectedComponent { @ Input ( ) captchaSiteKey : string = null ; captchaToken : string = null ; captcha : CaptchaIFrame ; constructor ( protected environmentService , protected i18nService , protected platformUtilsService ) { } async setupCaptcha ( ) { const webVaultUrl = this . environmentService . getWebVaultUrl ( ) ; this . captcha = new CaptchaIFrame ( window , webVaultUrl , this . i18nService , ( token ) => { this . captchaToken = token ; } , ( error ) => { this . platformUtilsService . showToast ( "error" , this . i18nService . t ( "errorOccurred" ) , error ) ; } , ( info ) => { this . platformUtilsService . showToast ( "info" , this . i18nService . t ( "info" ) , info ) ; } ) ; } showCaptcha ( ) { return ! Utils . isNullOrWhitespace ( this . captchaSiteKey ) ; } protected handleCaptchaRequired ( response ) { if ( Utils . isNullOrWhitespace ( response . captchaSiteKey ) ) { return false ; } this . captchaSiteKey = response . captchaSiteKey ; this . captcha . init ( response . captchaSiteKey ) ; return true ; } }