【问题标题】:Firebase Phone authentication and LinkingFirebase 电话身份验证和链接
【发布时间】:2017-10-16 05:23:33
【问题描述】:

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

  1. 用户输入电子邮件地址和密码。
  2. 然后我打电话给firebase.auth().createUserWithEmailAndPassword(values.email, values.password)
  3. 然后我需要将当前帐户与电话号码关联,所以我使用firebase.auth().currentUser.linkWithPhoneNumber("+xxxxxxxxxx", xxx)

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

请在下面找到我的代码:

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)
    });

【问题讨论】:

    标签: javascript reactjs firebase firebase-authentication


    【解决方案1】:

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

    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.
      });
    

    【讨论】:

    • 第二个参数auth.ApplicationVerifier是什么?我没看到
    • 这是 reCAPTCHA 验证器。这类似于signInWithPhoneNumber 流程。从official docs 了解更多信息。
    • 我一直在尝试解决这个问题,但这似乎是唯一有 linkWithPhoneNumber 示例的地方。我使用这种方法,但它说凭据已经与另一个帐户相关联,但事实并非如此,它(电话号码帐户)只有电话作为提供者。你们设法解决了这个问题还是这是一个错误? (我尝试了赏金但没有得到答案,所以我在这里。)。无论如何,我都找不到将帐户(之前使用电话号码登录)与创建电子邮件帐户并想要链接其号码 (updatePhonenumber) 的同一用户关联。
    猜你喜欢
    • 2021-07-13
    • 1970-01-01
    • 2019-08-05
    • 1970-01-01
    • 2021-03-10
    • 2021-06-10
    • 2019-05-20
    相关资源
    最近更新 更多