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

Angular 18 Support #324

Open
ant-ia opened this issue May 23, 2024 · 17 comments
Open

Angular 18 Support #324

ant-ia opened this issue May 23, 2024 · 17 comments

Comments

@ant-ia
Copy link

ant-ia commented May 23, 2024

Hi @DethAriel ,

when will angular 18 support be available?

Thank you for your work.

@LakhveerChahal
Copy link

Waiting for ng-recaptcha's Angular 18 support as it is causing difficulties in upgrading the project.
Thank you.

LakhveerChahal added a commit to LakhveerChahal/ng-recaptcha-2 that referenced this issue May 25, 2024
@LakhveerChahal
Copy link

Raised a pull request to upgrade the library to Angular v18
#325

@ant-ia
Copy link
Author

ant-ia commented Jun 3, 2024

Hi @DethAriel is there any update?
thanks

@vaibhavkumar-sf
Copy link

Waiting for it,

@pinpins
Copy link

pinpins commented Jun 14, 2024

Raised a pull request to upgrade the library to Angular v18 #325

perhaps you could take over and publish your fork on https://www.npmjs.com/

Seems dev is not around anymore

@LakhveerChahal
Copy link

@pinpins Did the same. Published it as a new library under the name ng-recaptcha-2

@ReaperTech
Copy link

ReaperTech commented Jun 24, 2024

Using npm i ng-recaptcha-2 for the new library at https://www.npmjs.com/package/ng-recaptcha-2?activeTab=readme still gives dependency error:

Conflicting peer dependency: @angular/[email protected]
npm ERR! node_modules/@angular/core
npm ERR! peer @angular/core@"^17.0.0" from [email protected]

How can we install this new library for Angular 18 without errors?
EDIT:
npm i ng-recaptcha-2 works with the new library at https://www.npmjs.com/package/ng-recaptcha-2?activeTab=readme if you first uninstall the old library npm uninstall ng-recaptcha

@LakhveerChahal
Copy link

@ReaperTech Uninstalling the old library & then installing ng-recaptcha-2 is the right way to do it. You shouldn't keep both.

@malek-itani
Copy link

Hi @DethAriel is there any update to migrate for Angular18?

@adcore-dev
Copy link

@vibhukumarcgi
Copy link

Is there any update on it? It is restricting me to upgrade to Angular version 18.

@LakhveerChahal
Copy link

Is there any update on it? It is restricting me to upgrade to Angular version 18.

You can replace the library, if needed, to recaptcha-2 as mentioned above

@MatthieuOlenine
Copy link

@ReaperTech Uninstalling the old library & then installing ng-recaptcha-2 is the right way to do it. You shouldn't keep both.

Did not work for me, still no library for captcha angular 18 today ?

@Ben555555
Copy link

Ben555555 commented Sep 5, 2024

@MatthieuOlenine
You can create your own service pretty easily (At least for v3):
https://ben-5.azurewebsites.net/2024/9/5/google-recaptcha-v3-with-angular/

@ccaiza
Copy link

ccaiza commented Oct 17, 2024

//here change code
import { DOCUMENT } from '@angular/common';
import { Inject, Injectable } from '@angular/core';
import { Observable } from 'rxjs';

declare var grecaptcha: any;

@Injectable({
providedIn: 'root'
})
export class RecaptchaService {

// private siteKey: string | null = null;
private loaded: boolean = false;
private scriptElement: HTMLScriptElement | null = null;

constructor(@Inject(DOCUMENT) private readonly document: Document,
@Inject('RECAPTCHA_SITE_KEY') private readonly siteKey: string) {
// this.siteKey = siteKey;
this.addRecaptchaScript();
}

/**

  • Agrega el script de google a la pagina.
  • @see https://developers.google.com/recaptcha/docs/v3?hl=es-419
  • @returns
    */
    private addRecaptchaScript(): void {
    // this.siteKey = siteKey;
    if (this.loaded) {
    return;
    }
    this.scriptElement = this.document.createElement('script');
    this.scriptElement.src = https://www.google.com/recaptcha/api.js?render=${this.siteKey};
    this.scriptElement.async = true;
    this.scriptElement.defer = true;
    this.document.head.appendChild(this.scriptElement);
    this.loaded = true;
    }

/**

  • Ocula el boton de recaptcha.
    */
    public removeRecaptchaScript(): void {
    const script = this.document.querySelector(script[src="https://www.google.com/recaptcha/api.js?render=${this.siteKey}"]);
    console.log("aqui vamos a cerrar el dato --->");
    console.log(script);
    if (script) {
    (script as HTMLScriptElement).remove();
    this.loaded = false;
    console.log("Script eliminado correctamente");

    // Eliminar el div de reCAPTCHA
    const recaptchaBadge = this.document.querySelector('.grecaptcha-badge');
    if (recaptchaBadge) {
    (recaptchaBadge as HTMLElement).remove();
    console.log("Div de reCAPTCHA eliminado correctamente");
    }
    } else {
    console.log("No se encontró el script para eliminar");
    }
    }

/**

  • Obtiene el token de acceso desde google recaptcha.

  • @param action Motivo

  • @returns Observable con el token
    */
    public execute(action: string): Observable {
    return new Observable(observer => {
    if (typeof grecaptcha === 'undefined') {
    observer.error('reCAPTCHA not loaded');
    return;
    }

    grecaptcha.ready(() => {
    grecaptcha.execute(this.siteKey, { action }).then((token: string) => {
    observer.next(token);
    observer.complete();
    }).catch((err: any) => {
    observer.error(err);
    });
    });
    });
    }

// public execute(action: string, callback: (token: string) => void): void {
// if (!this.siteKey) {
// throw new Error('Recaptcha site key is not set.');
// }

// grecaptcha.ready(() => {
// grecaptcha.execute(this.siteKey!, { action })
// .then(callback);
// });
// }
}

And componente

@component({
selector: 'app-pages-login',
standalone: true,
imports: [ FormsModule, RouterLink
],
providers:[
RecaptchaService,
{ provide: 'RECAPTCHA_SITE_KEY', useValue: environment.googlePublic}
],
templateUrl: './xxx.html',
styleUrls: ['./xxx.scss'],
})
export class YourComponent implements OnInit, AfterViewInit,OnDestroy {
constructor( private readonly recaptchaV3Service: RecaptchaService){
}

ngOnDestroy(): void {
this.recaptchaV3Service.removeRecaptchaScript();
}

ypuMethod(){
this.recaptchaV3Service.execute('validarClave')
.pipe(
//da de baja los observables para evitar perdida de memoria
takeUntil(this.unsubcribe$)
).subscribe({
next:nn=>{
},error:err=>{
}
});
}
}

@junaidjibran-bnb
Copy link

@MatthieuOlenine You can create your own service pretty easily (At least for v3): https://ben-5.azurewebsites.net/2024/9/5/google-recaptcha-v3-with-angular/

is its works?

@pinpins
Copy link

pinpins commented Nov 21, 2024

Repackaged to support also Angular 19

https://www.npmjs.com/package/ng-recaptcha-19

https://github.com/pinpins/ng-recaptcha-2/commit/6953fea753acd1567df91f15f23657f82753e605

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

No branches or pull requests