Google ReCaptcha components for Angular 6+


Google reCaptcha implementation for Angular 6 and beyond.

Features of Google reCaptcha Angular 6

  1. re Captcha v2
  2. re Captcha v3 (beta)
  3. invisible re Captcha

reCaptcha v3 Angular 6

This is the implementation of beta version of google re Captcha v3 as per following documentation.

First you need to inject the class in your component / service and then use Execute method with your action. Once you have the token, you need to verify it on your backend to get meaningful results. See official google documentation for more details.

import { ReCaptchaV3Service } from 'ngx-captcha';

 constructor(
   private reCaptchaV3Service: ReCaptchaV3Service
 ) { }

 ....

 this.reCaptchaV3Service.execute(this.siteKey, 'homepage', (token) => {
   console.log('This is your token: ', token);
 }, {
     useGlobalDomain: false
 });

Once you have done that, the rest is simple:

// app.component.ts
import { Component } from @angular/core;
@Component({
selector: my-app,
template: `<re-captcha (resolved)=resolved($event) siteKey=YOUR_SITE_KEY></re-captcha>`,
}) export class MyApp {
resolved(captchaResponse: string) {
console.log(`Resolved captcha with response: ${captchaResponse}`);
}

Supported versions

  1. For Angular 6 use ngx-captcha on version 5.0.4
  2. For Angular 7 use ngx-captcha on version >= 6.0.0

Installation of Google ReCaptcha

npm install ngx-captcha

Import NgxCaptchaModule to your module (i.e. AppModule)

Use with Angular 6 forms of Google reCaptcha

Depending on whether you want to use reactive forms or template-driven forms you need to include the appropriate modules, too. Add ReactiveFormsModule to your imports in case you want to use reactive forms. If you prefer the the template-driven approach simple add the FormsModule to your module.

Both can be imported from @angular/forms. In the demo project you can check out the normal demo to see how to use reactive forms or the invisible demo to see what the template-driven approach looks like. With the template-driven approach you don’t necessarily need to use a from element to wrap the component, you can instead use the [ngModelOptions]="{ standalone: true }". However, using it with the standalone option is not recommended but can be used if needed.

import { NgModule } from '@angular/core';
import { ReactiveFormsModule } from '@angular/forms';
import { NgxCaptchaModule } from 'ngx-captcha';

@NgModule({
  imports: [
    ReactiveFormsModule,
    NgxCaptchaModule
  })

export class AppModule { }
For More Angular Jquery Plugins Click


 

Leave a Reply

Your email address will not be published. Required fields are marked *