【问题标题】:How to implement reCaptcha with firebase in angular如何在 Angular 中使用 firebase 实现 reCaptcha
【发布时间】:2021-02-16 09:07:50
【问题描述】:

我正在尝试使用 angular 的 firebase 实现 recaptcha,但出现以下错误。

ERROR TypeError: Cannot read property 'RecaptchaVerifier' of undefined

在 .ts 文件中,我正在尝试以下操作:

import firebase from 'firebase/app';

recaptchaVerifier: firebase.auth.RecaptchaVerifier;

constructor(private windowService: WindowService) {}

ngOnInit() {
   this.windowRef = this.windowService.windowRef;
}

ngAfterViewInit() {
   this.recaptchaVerifier = new firebase.auth.RecaptchaVerifier('recaptcha-container', {
      size: 'normal',
      callback: (response) => {
             ...
         }
      });
      this.windowRef.recaptchaVerifier = this.recaptchaVerifier;
      this.windowRef.recaptchaVerifier.render();
}

窗口服务

@Injectable({
   providedIn: 'root'
})
export class WindowService {

   get windowRef() {
       return window;
   }
}

在模板中:

<div id="recaptcha-container"></div>

我做错了什么。 提前感谢您的任何回复!

【问题讨论】:

    标签: angular firebase firebase-authentication recaptcha angularfire2


    【解决方案1】:

    最后我将 angular/fire 和 firebase 降级为:

    "@angular/fire": "^6.0.0",
    "firebase": "^7.14.0",
    

    在这种情况下,导入 import * as firebase from 'firebase'; 有效,我可以使用 RecaptchaVerifier,例如:

    recaptchaVerifier: firebase.auth.RecaptchaVerifier;
    
    ngOnInit() {
       this.windowRef = this.windowService.windowRef;
    }
    
    ngAfterViewInit() {
       let captchaElement = document.getElementById('recaptcha-container');
       if (captchaElement != null) {
          this.recaptchaVerifier = new firebase.auth.RecaptchaVerifier('recaptcha-container',    
             {
                size: 'normal',
                callback: (response) => {
                   this.disableSignUpBtn = false;
                }
             });
             this.windowRef.recaptchaVerifier = this.recaptchaVerifier;
             this.windowRef.recaptchaVerifier.render();
          }
     }
    

    【讨论】:

      【解决方案2】:

      您需要从 firebase 导入所有内容,目前不包括 .auth。 试试这个:

      import * as firebase from 'firebase/app';
      而不是
      import firebase from 'firebase/app';

      【讨论】:

      • 感谢您的回复。我已经尝试过了,在这种情况下,在 VSCode 中 auth 在 firebase.auth.RecaptchaVerifier('recaptcha-container') 中加了下划线,并且应用程序不符合要求。那么如何导入 auth 和 RecaptchaVerifier?
      猜你喜欢
      • 2017-11-20
      • 2019-12-07
      • 2020-06-25
      • 1970-01-01
      • 2019-05-04
      • 2019-01-17
      • 2021-04-25
      • 2020-10-24
      • 1970-01-01
      相关资源
      最近更新 更多