首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用Firebase的'verifyPhoneNumber()‘来确认电话#的所有权而不使用#登录?

如何使用Firebase的'verifyPhoneNumber()‘来确认电话#的所有权而不使用#登录?
EN

Stack Overflow用户
提问于 2020-01-17 01:16:51
回答 5查看 18.5K关注 0票数 8

我在一个项目中使用react-native-firebase v5.6。

目标:在注册流程中,我有用户输入他们的电话号码,然后我发送一个OTP到所说的电话号码。我希望能够将用户输入的代码与从Firebase发送的代码进行比较,以便能够将条目授予注册的下一个步骤。

问题:用户获得SMS OTP和所有内容,但是phoneAuthSnapshot对象由firebase.auth().verifyPhoneNumber(number).on('state_changed', (phoneAuthSnapshot => {})返回,它没有给出firebase发送的代码的值,因此没有什么可以与用户输入的代码进行比较。但是,verificationId属性有一个值。下面是从上面的方法返回的对象:

代码语言:javascript
复制
'Verification code sent', { 
  verificationId: 'AM5PThBmFvPRB6x_tySDSCBG-6tezCCm0Niwm2ohmtmYktNJALCkj11vpwyou3QGTg_lT4lkKme8UvMGhtDO5rfMM7U9SNq7duQ41T8TeJupuEkxWOelgUiKf_iGSjnodFv9Jee8gvHc50XeAJ3z7wj0_BRSg_gwlN6sumL1rXJQ6AdZwzvGetebXhZMb2gGVQ9J7_JZykCwREEPB-vC0lQcUVdSMBjtig',
  code: null,
  error: null,
  state: 'sent' 
}

以下是我在屏幕上的实现:

代码语言:javascript
复制
firebase
  .firestore()
  .collection('users')
  .where('phoneNumber', '==', this.state.phoneNumber)
  .get()
  .then((querySnapshot) => {
    if (querySnapshot.empty === true) {
      // change status
      this.setState({ status: 'Sending confirmation code...' });
      // send confirmation OTP
      firebase.auth().verifyPhoneNumber(this.state.phoneNumber).on(
        'state_changed',
        (phoneAuthSnapshot) => {
          switch (phoneAuthSnapshot.state) {
            case firebase.auth.PhoneAuthState.CODE_SENT:
              console.log('Verification code sent', phoneAuthSnapshot);
              this.setState({ status: 'Confirmation code sent.', confirmationCode: phoneAuthSnapshot.code });

              break;
            case firebase.auth.PhoneAuthState.ERROR:
              console.log('Verification error: ' + JSON.stringify(phoneAuthSnapshot));
              this.setState({ status: 'Error sending code.', processing: false });
              break;
          }
        },
        (error) => {
          console.log('Error verifying phone number: ' + error);
        }
      );
    }
  })
  .catch((error) => {
    // there was an error
    console.log('Error during firebase operation: ' + JSON.stringify(error));
  });

如何使从Firebase发送的代码能够进行比较?

EN

Stack Overflow用户

发布于 2021-08-01 05:40:07

为了使用多因素身份验证,您必须在后台创建电话登录提供商,与主(在您的情况下)电子邮件登录提供商一起使用,而用户选择更新设置并启用MFA。然后在用户使用电子邮件登录提供者登录时链接它,如下所示;

代码语言:javascript
复制
const credential = auth.PhoneAuthProvider.credential(verificationId, code);
let userData = await auth().currentUser.linkWithCredential(credential);
票数 1
EN
查看全部 5 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59779991

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档