Google reCaptcha implementation for Angular 6 and beyond.
Features:
Live examples: https://enngage.github.io/ngx-captcha/
npm install ngx-captcha
Import NgxCaptchaModule
to your module (i.e. AppModule
)
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 { }
The configuration properties are a copy of the official ones that google provides. For explanation of what these properties do and how to use them, please refer to https://developers.google.com/recaptcha/docs/display and https://developers.google.com/recaptcha/docs/invisible.
your.component.ts
export class YourComponent implements OnInit {
protected aFormGroup: FormGroup;
constructor(private formBuilder: FormBuilder) {}
ngOnInit() {
this.aFormGroup = this.formBuilder.group({
recaptcha: ['', Validators.required]
});
}
}
your.template.html
<form [formGroup]="aFormGroup">
<ngx-recaptcha2 #captchaElem
[siteKey]="siteKey"
(reset)="handleReset()"
(expire)="handleExpire()"
(load)="handleLoad()"
(success)="handleSuccess($event)"
[useGlobalDomain]="false"
[size]="size"
[hl]="lang"
[theme]="theme"
[type]="type"
formControlName="recaptcha">
</ngx-recaptcha2>
</form>
This is the implementation of beta version of google reCaptcha v3 as per following documentation"https://developers.google.com/recaptcha/docs/v3".
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
});
<form [formGroup]="aFormGroup">
<ngx-invisible-recaptcha #captchaElem
[siteKey]="siteKey"
(reset)="handleReset()"
(ready)="handleReady()"
(load)="handleLoad()"
(success)="handleSuccess($event)"
[useGlobalDomain]="false"
[type]="type"
[badge]="badge"
[ngModel]="recaptcha"
[ngModelOptions]="{ standalone: true }">
</ngx-invisible-recaptcha>
</form>
Unit testing in Angular is possible, but a bit clunky because this component tries to dynamically include google's script if its not already loaded. You are not required to include in globally or manually which has a benefit of not loading until you actually use this component. This has a caveat though, since the load
callback is executed outside of Angular's zone, performing unit tests might fail due to racing condition where Angular might fail the test before the script has a chance to load and initialize captcha.
A simple fix for this is wait certain amount of time so that everything has a chance to initialize. See example below:
beforeEach(() => {
fixture = TestBed.createComponent(YourComponent);
component = fixture.componentInstance;
setTimeout(function () {
fixture.detectChanges();
}, 2000);
});
Other possible scenario might be including google's script globally. If someone has a better solution, please do let me know or submit a PR for a change in readme.
Under projects\ngx-captcha-lib
run
npm run publish-lib
Under root, generate demo app with
npm run build-demo-gh-pages
npx ngh --dir=dist-demo
ngx-weui 是一个使用 Angular 构建的 WeUI 组件。 在线示例以及API文档。
ngx-fastdfs 是 nginx + lua +fastdfs 实现分布式图片实时动态压缩。 install 进入docker目录docker build -t fastdfs:dev . 使用 docker -idt -p 80:80 fastdfs:dev /bin/bash进入容器执行/etc/rc.local 测试 进入容器执行test目录下的./test.sh或者直接执行下面脚本
ngx-markdown ngx-markdown is an Angular library that combines... Marked to parse markdown to HTML Prism.js for language syntax highlight Emoji-Toolkit for emoji support KaTeX for math expression rende
ngx-admin Who uses ngx-admin?| Documentation | Installation Guidelines | Angular templates New! Material theme for ngx-admin Material admin theme is based on the most popular Angular dashboard templat
@sweetalert2/ngx-sweetalert2 Official SweetAlert2 integration for Angular This is not a regular API wrapper for SweetAlert (which already works very well alone), it intends to provide Angular-esque ut
ngx-dropzone A lightweight and highly customizable Angular dropzone component for file uploads. For a demo see DEMO. And the CODE for the demo. Install $ npm install --save ngx-dropzone Usage // in ap
ngx-slick Support angular 6+, Slick 1.8.1 Example Installation To install this library, run: $ npm install ngx-slick --save Consuming your library Once you have published your library to npm, you can
Angular Module for displaying a feed of items in a masonry layout using https://github.com/desandro/masonry This package was originally a fork from https://github.com/jelgblad/angular2-masonry to allo