Adam
Adam

Reputation: 361

Angular Google Recaptcha | How to get recaptcha token result

I'm using Angular Google Recaptcha with my Angular 7 project and it's working after I install it. But the problem is I don't see how to get the Recaptcha token back in the document they've provided. Does anyone know?

I have seen only two functions so far in the document. Is this the full working package?

Upvotes: 0

Views: 743

Answers (1)

Sebastian Puchet
Sebastian Puchet

Reputation: 398

According to the documentation you can use the package with reactive forms and template-driven forms. They also provide two examples in https://github.com/JamesHenry/angular-google-recaptcha#readme.

If you go for the reactive form approach, you can get the token from the FormControl value, something like this:

import { Component, On Init } from '@angular/core';
import { FormControl } from '@angular/forms';

@Component({
    selector: 'app',
    template: `
        <recaptcha
          [formControl]="myRecaptcha"
          (scriptLoad)="onScriptLoad()"
          (scriptError)="onScriptError()"
        ></recaptcha>
    `
})
export class AppComponent implements onInit {
    myRecaptcha = new FormControl(false);

    ngOnInit() {
        this.myRecaptcha.valueChanges.subscribe(token => console.log(token));
    }

    onScriptLoad() {
        console.log('Google reCAPTCHA loaded and is ready for use!')
    }

    onScriptError() {
        console.log('Something went long when loading the Google reCAPTCHA')
    }
}

Upvotes: 2

Related Questions