代码之家  ›  专栏  ›  技术社区  ›  Taylor Austin

用户正在使用电话号码登录,即使他们没有在firebase中使用react提供sms代码

  •  0
  • Taylor Austin  · 技术社区  · 8 年前

    我在这里遇到了一个小问题。在这些场景中,一切都很好:

    1. 用户登录并拥有启用了sms的电话号码,获取验证码并输入正确的代码。用户已登录。

    2. 用户登录并拥有禁用短信的电话号码。用户已登录,无需任何sms进程。

    3. 用户登录并且没有电话号码。用户已登录,无需任何sms进程。

    什么工作不正常:

    1. 用户登录并拥有启用了短信的电话号码,输入了错误的验证码。用户未登录。这就是我想要的工作方式。现在,如果用户停留在登录页面上,并使用其电子邮件和密码登录,就好像跳过了整个电话过程,并将其登录。然而,它并不像 signInWithPhoneNumber 过程

    例如,在此场景中,应通过以下方式将用户重定向到主页 this.setPage('home') ,但它从未到达那里,就像我说的那样,它似乎被取消了,但用户已登录。


    编辑:

    关于这个问题的更多细节。如果你看一下代码,你会发现无论用户在什么情况下都会被提醒,无论他是否使用短信登录。此外,任何错误也会通过catch块发出警报。我很难找到这个错误的原因是因为屏幕上没有警报,就好像它通过登录一样 使用电话号码登录 ,但承诺被取消了,没有其他事情发生。

    当用户登录时,应该发生两件事,一件是用户应该得到一个弹出窗口,显示他们是否使用短信登录,另一件是用户应该被重定向到主页。这两种方法都适用于所有前三种情况,但第四种senario没有弹出窗口,也没有重定向。这就好像用户登录后,代码在某个地方被停止了一样。


    swal的意思是sweetalert。

    代码如下:

    login(event) {
        event.preventDefault();
        const email = this.loginEmail.value;
        const password = this.loginPsw.value;
        const appVerifier = window.recaptchaVerifier;
    
        if (!this.verify) {
          swal({
            title: 'Oops...',
            text: 'You forgot to complete the reCAPTCHA',
            icon: 'error'
          });
    
          return;
        }
    
        fire
          .auth()
          .signInWithEmailAndPassword(email, password)
          .then(user => {
            if (user.phoneNumber) {
              fire
                .database()
                .ref('2FA/' + user.uid)
                .once('value', snap => {
                 // --> even if snap.val() is true after user puts in wrong sms 
                 // this value does not matter it passes true , 
                 // but then I do not see any of the promise sequence happen from 
                 // signInWithPhoneNumber
                  if (snap.val() === true) {
                    fire
                      .auth()
                      .signInWithPhoneNumber(`${user.phoneNumber}`, appVerifier)
                      .then(confirmationResult => {
                        if (confirmationResult) {
                          swal({
                            closeOnClickOutside: false,
                            closeOnEsc: false,
                            title: 'Success',
                            text: 'Please provide use the verification code to continue',
                            icon: 'success',
                            buttons: true,
                            dangerMode: false,
                            content: {
                              element: 'input',
                              attributes: {
                                placeholder: 'Confirmation code here',
                                type: 'text'
                              }
                            }
                          })
                            .then(value => {
                              if (!value) {
                                throw new Error('Must provide a code to login.');
                                return;
                              }
    
                              return confirmationResult.confirm(value);
                            })
                            .then(response => {
                              return fire.auth().signInWithEmailAndPassword(email, password);
                            })
                            .then(user => {
                              swal({
                                title: 'Sucess',
                                text: `${user.email} signed in with sms verification`,
                                icon: 'success'
                              });
    
                              this.props.setPage('home');
                            })
                            .catch(err => {
                              fire.auth().signOut();
                              this.loginForm.reset();
    
                              swal({ title: 'Oops...', text: `${err}`, icon: 'error' });
                            });
                        }
                      })
                      .catch(err => {
                        fire.auth().signOut();
                        this.loginForm.reset();
    
                        swal({ title: 'Oops...', text: `${err}`, icon: 'error' });
                      });
                  }
                });
    
              return;
            }
            swal({
              title: 'Success',
              text: `${user.email} signed in without sms verification.`,
              icon: 'success'
            });
            this.props.setPage('home');
          })
          .catch(err => {
            fire.auth().signOut();
    
            swal({
              title: 'Oops...',
              text: `${err}`,
              icon: 'error'
            });
          });
      }
    
    1 回复  |  直到 8 年前
        1
  •  0
  •   Taylor Austin    8 年前

    事实证明,问题是在这种情况发生后:

    User signs in and has a phone number with sms enabled, puts in wrong verification code... (有关这件事的更多信息,请查看问题)

    这个 appVerifier 无效,因为验证码在失败后未重置。所以从技术上讲,有一个有效的验证码,但我想这不是一个新的验证码,这导致了代码的sms部分无法发生。我不知道为什么它没有抛出错误,但无论如何 catch 块,共块 signInWithPhone 承诺我只需要补充:

    window.recaptchaVerifier.render().then(widgetId => {
      window.recaptchaVerifier.reset(widgetId);
    });
    

    这就解决了它。