Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: safe destroy of hcaptcha instance #72

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

DSergiu
Copy link

@DSergiu DSergiu commented Oct 12, 2023

  • for a safe cleanup it is recommended to use hcaptcha.remove(hCaptchaId)

@degloman
Copy link

@DSergiu
I've created a temporary directive to handle such case. But I faced with issue when you navigate to next page and get back to page with captcha again.

image

///<reference types="@hcaptcha/types"/>

import { DOCUMENT } from '@angular/common';
import { Directive, inject, OnDestroy } from '@angular/core';

import { NgHcaptchaComponent } from 'ng-hcaptcha';

declare global {
  // eslint-disable-next-line @typescript-eslint/naming-convention
  interface Window {
    hcaptcha: HCaptcha;
  }
}

@Directive({
  selector: 'ng-hcaptcha[hcaptchaSafeDestroy]',
  standalone: true
})
export class HcaptchaSafeDestroyDirective implements OnDestroy {
  protected readonly document = inject(DOCUMENT);
  protected hCaptchaComponent = inject(NgHcaptchaComponent, { optional: true });

  get window(): Window {
    return this.document.defaultView;
  }

  get widgetId(): string {
    return (this.hCaptchaComponent as unknown as { widgetId: string })?.widgetId;
  }

  ngOnDestroy(): void {
    if (this.window.hcaptcha && this.widgetId) {
      this.window.hcaptcha.remove(this.widgetId);
    }
  }
}

this place will throw an error: https://github.com/leNicDev/ng-hcaptcha/blob/3ba5c8d7491cc41a80c2d934097b8c1a1fe07e1e/projects/ng-hcaptcha/src/lib/ng-hcaptcha.component.ts#L119C1-L119C1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants