Firebase Phone身份验证和链接 [英] Firebase Phone authentication and Linking

查看:87
本文介绍了Firebase Phone身份验证和链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我的电话号码与我的电子邮件密码验证相关联。所以我使用以下步骤建立我的注册:

i'm trying to link my phone number with my email password authentication. So i build my registration using below steps:


  1. 用户输入电子邮件地址和密码。

  2. 然后我打电话给 firebase.auth()。createUserWithEmailAndPassword(values.email,values.password)

  3. 然后我需要链接当前帐户电话号码所以我正在使用 firebase.auth()。currentUser.linkWithPhoneNumber(+ xxxxxxxxxx,xxx)

  1. User enters email address and password.
  2. then i call firebase.auth().createUserWithEmailAndPassword(values.email, values.password)
  3. then I need to link current account with phone number so I'm using firebase.auth().currentUser.linkWithPhoneNumber("+xxxxxxxxxx", xxx)

但是,我没有看到任何链接。在我的firebase控制台中创建了2个帐户,当前用户只在其详细信息中有电话号码。当我再次使用电子邮件和密码登录并查看用户详细信息时,电话号码不存在!!!

however, i don't see any linking. 2 accounts created in my firebase console and the current user have phone number only in his details. When I log in with email and password again and check user details the phone number is not there !!!

请在下面找到我的代码:

Please find my code below:

onSubmit(values) {
    this.props.firebase.auth().createUserWithEmailAndPassword(values.email, values.password).then((user) => {
        //send recaptchaverifier
        window.recaptchaVerifier.verify();

    }).catch((error) => {
        console.log(error);
    });
}


window.recaptchaVerifier = new firebase.auth.RecaptchaVerifier('submit-button', {
        'size': 'invisible',
        'callback': function(response){
         //called when we call "window.recaptchaverifier.verify() in 
         //onSubmit function
            var xxx = window.recaptchaVerifier;

            this.props.firebase.auth().currentUser.linkWithPhoneNumber("+xxxxxxxx", xxx)
                .then((verificationId) => {
                    console.log('inside');
                    console.log(resp);
                    var verificationCode = window.prompt('Please enter the verification ' +
                        'code that was sent to your mobile device.');
                    return firebase.auth.PhoneAuthProvider.credential(resp.verificationId,
                        verificationCode);
                }).then((phoneCredential) => {
                console.log("RESULT OF AUTH", phoneCredential);
                console.log("USER INFO: ", this.props.firebase.auth().currentUser);
                return this.props.firebase.auth().signInWithCredential(phoneCredential)
            }).catch((error) => {
                console.log("ERRORS: ", error);
            }).catch((error) => {
                console.log("ERROR", error)
            });
        }.bind(this)
    });


推荐答案

您正在调用 signInWithCredential 使用创建新用户的电话凭证。您需要执行以下操作:

You are calling signInWithCredential using the phone credential which creates a new user. You need to do the following:

firebase.auth().currentUser.linkWithPhoneNumber("+xxxxxxxx", xxx)
  .then((confirmationResult) => {
    // At this point SMS is sent. Ask user for code.
    let code = window.prompt('Please enter the 6 digit code');
    return confirmationResult.confirm(code);
  })
  .then((result) {
    // Phone credential now linked to current user.
    // User now can sign in with email/pass or phone.
  });
  .catch((error) => {
    // Error occurred.
  });

这篇关于Firebase Phone身份验证和链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆