Firebase Phone authentication and Linking

徘徊边缘 提交于 2019-12-18 13:49:10

问题


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

  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)

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

回答1:


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


来源:https://stackoverflow.com/questions/46763427/firebase-phone-authentication-and-linking

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!