用于创建电话身份验证的Firebase Auth SMS验证码

时间:2018-05-09 12:19:03

标签: firebase firebase-authentication

我使用firebase构建本机反应,并使用插件react-native-firebase。 我实现了firebase auth手机,当我将短信代码发送到我的手机时,我得到了2个错误:

  1. 当我输入正确的短信代码时我收到错误
  2.   

    用于创建手机身份验证凭据的短信验证码无效。请重新发送验证码短信并

    1. 当我重新加载应用程序然后想再次输入代码时出现错误
    2.   

      短信代码已过期。请重新发送验证码以重试

      我该如何解决这些问题? 另外,还有内置发送重发短信吗?

      这是我的代码

      confirmPhone = async (phoneNumber) => {
          return new Promise((res, rej) => {
              firebase.auth().verifyPhoneNumber(phoneNumber)
                  .on('state_changed', async (phoneAuthSnapshot) => {
                      switch (phoneAuthSnapshot.state) {
                      case firebase.auth.PhoneAuthState.AUTO_VERIFIED:
                          await this.confirmCode(phoneAuthSnapshot.verificationId, phoneAuthSnapshot.code, phoneAuthSnapshot)
                          res(phoneAuthSnapshot)
      
                          break
      
                      case firebase.auth.PhoneAuthState.CODE_SENT:
                          // await userSettings.set(AUTH_KEYS.VERIFICATION_ID, phoneAuthSnapshot.verificationId)
                          UserStore.setVerificationId(phoneAuthSnapshot.verificationId)
                          res(phoneAuthSnapshot)
                          break
      
                      case firebase.auth.PhoneAuthState.AUTO_VERIFY_TIMEOUT: // or 'timeout'
                          UserStore.setVerificationId(phoneAuthSnapshot.verificationId)
                          res(phoneAuthSnapshot)
      
      
                      case firebase.auth.PhoneAuthState.ERROR:
                          UserStore.setErrorConfirmationCode(phoneAuthSnapshot.error)
                          rej(phoneAuthSnapshot)
                          break
      
                      }
                  })
          })
      }
      
      confirmCode = async (verificationId, code, phoneAuthSnapshot) => {
          UserStore.setCodeInput(code)
          try{
              const credential = await firebase.auth.PhoneAuthProvider.credential(UserStore.verificationId, code)
              UserStore.setUserCredentials(credential)
              AppStore.setAlreadyRegister(true)
              this.authenticate(credential)
              return credential
          }catch(e){
              console.log(e)
          }
      }
      
      authenticate = (credential) => {
          firebase.auth().signInAndRetrieveDataWithCredential(credential)
      
      }
      

0 个答案:

没有答案
相关问题