-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(demo): add demo examples of reCAPTCHA v3 API usage
- Loading branch information
Showing
4 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<recaptcha-demo-wrapper> | ||
<button (click)="executeAction('homepage')">Execute "homepage" action</button> | ||
<button (click)="executeAction('shoppingCart')">Execute "shoppingCart" action</button> | ||
|
||
<h3>Most recent token</h3> | ||
<pre>{{ formatToken(recentToken) }}</pre> | ||
|
||
<h3>reCAPTCHA execution log</h3> | ||
<pre *ngIf="executionLog.length === 0">(empty)</pre> | ||
<ol *ngIf="executionLog.length > 0"> | ||
<li *ngFor="let entry of executionLog"> | ||
Action: <code>{{ entry.action }}</code>. Token: <code>{{ formatToken(entry.token) }}</code> | ||
</li> | ||
</ol> | ||
</recaptcha-demo-wrapper> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { Component, Inject, OnDestroy, OnInit } from '@angular/core'; | ||
|
||
import { OnExecuteData, ReCaptchaV3Service } from 'ng-recaptcha'; | ||
import { Subscription } from 'rxjs'; | ||
|
||
@Component({ | ||
selector: 'recaptcha-demo', | ||
templateUrl: './v3-demo.component.html', | ||
}) | ||
export class RecaptchaV3DemoComponent implements OnInit, OnDestroy { | ||
public recentToken: string = ''; | ||
public readonly executionLog: OnExecuteData[] = []; | ||
|
||
private allExecutionsSubscription: Subscription; | ||
private singleExecutionSubscription: Subscription; | ||
|
||
constructor( | ||
private recaptchaV3Service: ReCaptchaV3Service, | ||
) { | ||
} | ||
|
||
public executeAction(action: string): void { | ||
if (this.singleExecutionSubscription) { | ||
this.singleExecutionSubscription.unsubscribe(); | ||
} | ||
this.singleExecutionSubscription = this.recaptchaV3Service.execute(action) | ||
.subscribe((token) => this.recentToken = token); | ||
} | ||
|
||
public ngOnInit() { | ||
this.allExecutionsSubscription = this.recaptchaV3Service.onExecute | ||
.subscribe((data) => this.executionLog.push(data)); | ||
} | ||
|
||
public ngOnDestroy() { | ||
if (this.allExecutionsSubscription) { | ||
this.allExecutionsSubscription.unsubscribe(); | ||
} | ||
if (this.singleExecutionSubscription) { | ||
this.singleExecutionSubscription.unsubscribe(); | ||
} | ||
} | ||
|
||
public formatToken(token: string): string { | ||
if (!token) { | ||
return '(empty)'; | ||
} | ||
|
||
return `${token.substr(0, 7)}...${token.substr(-7)}`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { NgModule } from '@angular/core'; | ||
import { BrowserModule } from '@angular/platform-browser'; | ||
|
||
import { RECAPTCHA_V3_SITE_KEY, RecaptchaV3Module } from 'ng-recaptcha'; | ||
|
||
import { PAGE_SETTINGS } from '../../demo-wrapper/demo-wrapper.component'; | ||
import { DemoWrapperModule } from '../../demo-wrapper/demo-wrapper.module'; | ||
import { RecaptchaV3DemoComponent } from './v3-demo.component'; | ||
import { settings } from './v3-demo.data'; | ||
|
||
@NgModule({ | ||
bootstrap: [RecaptchaV3DemoComponent], | ||
declarations: [RecaptchaV3DemoComponent], | ||
imports: [ | ||
BrowserModule, | ||
RecaptchaV3Module, | ||
DemoWrapperModule, | ||
], | ||
providers: [ | ||
{ provide: PAGE_SETTINGS, useValue: settings }, | ||
{ provide: RECAPTCHA_V3_SITE_KEY, useValue: '6LeGCZAUAAAAADuhzcuvSB-lYDsxJBl9HUWtZkUM' }, | ||
], | ||
}) | ||
export class DemoModule { } |