Firebase Phone authentication and Linking
i'm trying to link my phone number with my email password authentication. So i build my registration using below steps:
firebase.auth().createUserWithEmailAndPassword(values.email, values.password)
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)
});
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.
});
链接地址: http://www.djcxy.com/p/32344.html
下一篇: Firebase电话身份验证和链接