Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x | import { ChangeDetectionStrategy, Component, DestroyRef, Input, NgModule, OnInit, inject } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { FormGroup, Validators } from '@angular/forms';
import { TranslateModule } from '@ngx-translate/core';
import { RECAPTCHA_V3_SITE_KEY, ReCaptchaV3Service, RecaptchaV3Module } from 'ng-recaptcha';
import { timer } from 'rxjs';
import { filter, switchMap, take } from 'rxjs/operators';
import { DirectivesModule } from 'ish-core/directives.module';
import { AppFacade } from 'ish-core/facades/app.facade';
import { whenTruthy } from 'ish-core/utils/operators';
import {
SitekeyProviderService,
getSynchronizedSiteKey,
} from '../../exports/sitekey-provider/sitekey-provider.service';
/**
* The Captcha V3 Component
*
* Displays a captcha widget (V3) and saves the response token in the given form. It should only be used by {@link CaptchaComponent}
*/
@Component({
selector: 'ish-captcha-v3',
templateUrl: './captcha-v3.component.html',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class CaptchaV3Component implements OnInit {
@Input({ required: true }) parentForm: FormGroup;
private destroyRef = inject(DestroyRef);
constructor(private recaptchaV3Service: ReCaptchaV3Service, private appFacade: AppFacade) {}
ngOnInit() {
this.parentForm.get('captchaAction').setValidators([Validators.required]);
// as soon as the app is getting stable the form requests a captcha token every 2 minutes
if (!SSR) {
this.appFacade.appBecameStable$
.pipe(
take(1),
switchMap(() =>
timer(0, 2 * (60 - 3) * 1000).pipe(
switchMap(() => this.recaptchaV3Service.execute(this.parentForm.get('captchaAction').value))
)
),
whenTruthy(),
takeUntilDestroyed(this.destroyRef)
)
.subscribe(token => {
this.parentForm.get('captcha').setValue(token);
this.parentForm.get('captcha').updateValueAndValidity();
});
// if the captcha is set to undefined from outside request a captcha token
this.parentForm
.get('captcha')
.valueChanges.pipe(
filter(token => token === undefined),
switchMap(() => this.recaptchaV3Service.execute(this.parentForm.get('captchaAction').value)),
whenTruthy(),
takeUntilDestroyed(this.destroyRef)
)
.subscribe(token => {
this.parentForm.get('captcha').setValue(token);
this.parentForm.get('captcha').updateValueAndValidity();
});
}
}
}
@NgModule({
imports: [DirectivesModule, RecaptchaV3Module, TranslateModule],
declarations: [CaptchaV3Component],
providers: [
{
provide: RECAPTCHA_V3_SITE_KEY,
useFactory: getSynchronizedSiteKey,
deps: [SitekeyProviderService],
},
],
})
export class CaptchaV3ComponentModule {}
|