在TypeScript中重置Google reCaptcha [英] Google reCaptcha reset in TypeScript

查看:63
本文介绍了在TypeScript中重置Google reCaptcha的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发Angular 2 + TypeScript应用程序.如果表单无效,我需要重设reCaptcha.但是在我写 grecaptcha.reset(); 的打字稿代码中,出现错误:无法解析符号'grecaptcha'

I'm developing Angular 2 + TypeScript app. I need to reset reCaptcha if form is invalid. But in my typescript code when I write grecaptcha.reset();, I get error: Can not resolve symbol 'grecaptcha'

如何重置reCaptcha或解析/声明 grecaptcha 变量以及在何处?

How can I reset reCaptcha or resolve/declare grecaptcha variable and where?

推荐答案

有关最新答案,请参见 将reCAPTCHA与角度4

For updated answer see Correct way to integrate reCAPTCHA with Angular 4

对于Typescript,声明 grecaptcha :

For Typescript, declare grecaptcha:

declare var grecaptcha: any;

这假定已包含 reCAPTCHA api

<script src='https://www.google.com/recaptcha/api.js?render=explicit'></script>

或者,可以如下键入 grecaptcha :

declare var grecaptcha: ReCAPTCHA;

ReCAPTCHA接口

import { ElementRef } from '@angular/core';

/**
 * Interface for Google's reCAPTCHA JavaScript API. 
 * 
 * Display API
 * @see {@link https://developers.google.com/recaptcha/docs/display}
 * 
 * Invisible API
 * @see {@link https://developers.google.com/recaptcha/docs/invisible}
 */
export interface ReCAPTCHA {
  
  /**
   * Programatically invoke the reCAPTCHA check. Used if the invisible reCAPTCHA is on a div 
   * instead of a button.
   * 
   * @param {string} opt_widget_id Optional widget ID, defaults to the first widget created if 
   *     unspecified.
   */
  execute(opt_widget_id?: string): void;

  /** 
   * Renders the container as a reCAPTCHA widget and returns the ID of the newly created widget.
   * 
   * @param {ElementRef|string} container The HTML element to render the reCAPTCHA widget.  Specify 
   *    either the ID of the container (string) or the DOM element itself. 
   * @param {Object} parameters An object containing parameters as key=value pairs, for example,
   *    {"sitekey": "your_site_key", "theme": "light"}.
   */
  render(container: ElementRef|string, parameters: {[key: string]: string}): void;

  /** 
   * Resets the reCAPTCHA widget.
   * 
   * @param {string} opt_widget_id Optional widget ID, defaults to the first widget created if 
   *     unspecified.
   */
  reset(opt_widget_id?: string): void;

  /** 
   * Gets the response for the reCAPTCHA widget. Returns a null if reCaptcha is not validated. 
   * 
   * @param {string} opt_widget_id Optional widget ID, defaults to the first widget created if 
   *     unspecified.
   */
  getResponse(opt_widget_id?: string): string;
}

示例

以下代码是概述,没有显示服务器端ReCAPTCHA验证.

Example

The following code is an outline and does not show the server side ReCAPTCHA verification.

import { Injectable, OnInit } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs';

export interface ReCAPTCHAResponse {
  success: boolean; 
  status_code: number,
  error_codes?: Array<string>;
}

@Injectable()
export class ReCAPTCHAService {
  public recaptchaResponse$: Observable<ReCAPTCHAResponse>;

  public constructor(private http: Http) {}

  public verifyUserResponse(userResponseToken: string): Observable<ReCAPTCHAResponse> {
    let headers = new Headers({ 'Content-Type': 'application/json' });
    let options = new RequestOptions({ headers: headers });

    return this.http.post('/auth/captcha', {'g-recaptcha-response': userResponseToken}, options)
        .map( (res: Response) => this.extractData(res))
        .catch( (error: Response | any) => this.handleError(error));
  }


  private extractData(res: Response): ReCAPTCHAResponse {
    const recaptchaResponse: ReCAPTCHAResponse = res.json();
    return recaptchaResponse;
  }


  private handleError (error: Response | any): Observable<ReCAPTCHAResponse> {
    let errMsg: string;
    if (error instanceof Response) {
      let body = error.json() || '';
      let err = body.error || JSON.stringify(body);
      errMsg = error.status + ' - ' + (error.statusText || '') + ': ' + err;
    } else {
      errMsg = error.message ? error.message : error.toString();
    }

    return Observable.throw({ success: false, status_code: 0, error_codes: [errMsg]});
  }
}

组件

import { Component, OnDestroy, OnInit } from '@angular/core';
import { ReCAPTCHA } from './recaptcha';
import { ReCAPTCHAResponse, ReCAPTCHAService } from './recaptcha.service';

declare var grecaptcha: ReCAPTCHA;
declare var window: any;

@Component ({
  moduleId: module.id,
  selector: 'create-account',
  templateUrl: 'create-account.component.html'
})
export class CreateAccountComponent implements OnDestroy, OnInit {

  public constructor(private recaptcha: ReCAPTCHAService, private formBuilder: FormBuilder) {}

  public ngOnInit(): void {
    this.buildForms();

    // Export reCAPTCHACallback to global scope.
    window['reCAPTCHACallback'] = this.reCAPTCHACallback.bind(this);

    grecaptcha.render('create-account-captcha', {
      'sitekey': 'your-site-key',
      'size': 'invisible',        // Optional (see docs)
      'callback': 'reCAPTCHACallback'
    });
  }

  /**
   * Verify reCAPTCHA response on server. 
   */
  public reCAPTCHACallback(token: string) {

    if (token == null /* checks for undefined or null */ || token.length === 0) {
      grecaptcha.reset();
      // TODO: report that captcha was invalid
    } else {
      let response$: Observable<ReCAPTCHAResponse>; 
      response$ = this.recaptcha.verifyUserResponse(token);
      response$.subscribe( r => {
        if (r.success) {
          // TODO: create the new user account
        } else {
          grecaptcha.reset();
          // TODO: report that captcha was invalid
        }
      },
      (error: any) => {
        // TODO: handle server error
      });
    }
  }

  private buildForms(): void {
    // TODO
  }
}

这篇关于在TypeScript中重置Google reCaptcha的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆