RN/Firebase:将电话凭证与现有帐户链接

RN/Firebase:将电话凭证与现有帐户链接,firebase,authentication,react-native,firebase-authentication,Firebase,Authentication,React Native,Firebase Authentication,我正在图书馆的React原生项目中使用Firebase。我正在尝试在入职期间获取用户电话号码。使用电子邮件/密码首次注册后,获取电话号码的流程为: 使用firebase.auth.verifyPhoneNumberphoneNumber输入并发送电话号码 接收验证码并确认 如果成功,请将电话号码添加到当前身份验证用户 因此,我的组件中有两种方法:sendCode将代码发送到提供的电话号码,verifyCode将代码输入与发送的代码进行比较 sendCode = () => { const

我正在图书馆的React原生项目中使用Firebase。我正在尝试在入职期间获取用户电话号码。使用电子邮件/密码首次注册后,获取电话号码的流程为:

使用firebase.auth.verifyPhoneNumberphoneNumber输入并发送电话号码 接收验证码并确认 如果成功,请将电话号码添加到当前身份验证用户 因此,我的组件中有两种方法:sendCode将代码发送到提供的电话号码,verifyCode将代码输入与发送的代码进行比较

sendCode = () => {

const { phoneNumber } = this.state
firebase.auth()
  .verifyPhoneNumber(phoneNumber)
  .on('state_changed', (phoneAuthSnapshot) => {

    switch (phoneAuthSnapshot.state) {

      case firebase.auth.PhoneAuthState.CODE_SENT:
        // This ends up creating a NEW user instead of adding phone number to the current user
        firebase.auth().signInWithPhoneNumber(phoneNumber)
          .then(confirmResult => this.setState({ confirmResult }))
          .catch(err => {console.log('some other error:', err)})
        break;

      case firebase.auth.PhoneAuthState.ERROR:
        console.log(phoneAuthSnapshot.error);
        break;

    }
  }, (error) => {
    console.log(error);
  }, (phoneAuthSnapshot) => {
    console.log(phoneAuthSnapshot);
  })
}  

verifyCode = () => {
    const { codeInput, confirmResult } = this.state;
    if (confirmResult && codeInput.length) {
      confirmResult.confirm(codeInput)
        .then(user => {
          console.log(user);
        })
        .catch(err => {console.log('error verifying code:', err)})
    }
}
接下来,我能够发送验证代码,但是Promise返回的是对象和对象,而不是函数,我需要在verifyCode中验证代码

该示例建议使用firebase.auth.signInWithPhoneNumberphoneNumber,然后返回一个函数来确认代码。这样做效果不好,因为它创建了一个新的身份验证用户,而不是将电话号码添加到当前用户。另一个问题是,用户体验到两个reCaptcha挑战,而不是一个


有什么建议吗?

以下是我的两种工作方法,可以将给定的电话号码链接到facebook或电子邮件/密码验证

verifyPhone: async phnumber => {
 try {
  let verify = await FirebaseAuth.auth()
    .verifyPhoneNumber(phnumber)
    .on("state_changed", phoneAuthSnapshot => {
      switch (phoneAuthSnapshot.state) {
        case FirebaseAuth.auth.PhoneAuthState.CODE_SENT:
          return phoneAuthSnapshot;
          break;
        case FirebaseAuth.auth.PhoneAuthState.ERROR:
          console.log(phoneAuthSnapshot.error);
          return null;
          break;
      }
    });
  return verify;
 } catch (error) {
   console.log(error);
 }
}
verifyPhone函数将接受电话号码并返回包含您的verificationId的对象。接下来是调用这个函数

processVerificationCode: async (verificationId, code) => {
 try {
  let credential = FirebaseAuth.auth.PhoneAuthProvider.credential(
    verificationId,
    code
  );
  let currentUser = FirebaseAuth.auth().currentUser;
  return currentUser.linkWithCredential(credential);
 } catch (error) {
   console.log(error);
 }
}
processVerificationCode函数将接受2个参数verificationId和输入的代码,然后调用PhoneAuthProvider获取电话号码凭据。下一步是获取当前登录用户(假设您已有登录),然后从该用户呼叫linkWithCredential并传递您的电话号码凭据


就这样。希望有帮助。

将生成的凭据传递给linkwithcredential时出错。