How can i update user phone number which it use for auth in firebase.auth??
Firebase give as method:
updatePhoneNumber(phoneCredential)
But we need give phoneCredential. This credential takes object:
interface AuthCredential {
providerId: string;
signInMethod: string;
}
What must be in this phoneCredential, i try to send
providerId: 'phone';
signInMethod: 'phone';
and this is not work ;(
The solution to updatePhoneNumber with a current user / logged-in account. In case this helps anyone.
problem: Say you have a current logged in account with any number of single/merged accounts and they want to update their number. How would they do it.
solution: The below is Auth for Web/JS method, the equivalent will exist for your target platform.
function updateProfilePhoneNumber() {
//country code plus your phone number excluding leading 0 if exists.
var phoneNumber = "+447xxxxxxxxx"; //you could provide a prompt/modal or other field in your UX to replace this phone number.
// let phoneNumber = "+441234567890"; //testing number, ideally you should set this in your firebase auth settings
// var verificationCode = "123456";
// Turn off phone auth app verification.
// firebase.auth().settings.appVerificationDisabledForTesting = true;
// This will render a fake reCAPTCHA as appVerificationDisabledForTesting is true.
// This will resolve after rendering without app verification.
var appVerifier = new firebase.auth.RecaptchaVerifier(
"recaptcha-container",
{
size: "invisible"
}
);
var provider = new firebase.auth.PhoneAuthProvider();
provider.verifyPhoneNumber(phoneNumber, appVerifier)
.then(function (verificationId) {
var verificationCode = window.prompt('Please enter the verification ' +
'code that was sent to your mobile device.');
phoneCredential = firebase.auth.PhoneAuthProvider.credential(verificationId, verificationCode);
user.currentUser.updatePhoneNumber(phoneCredential);
})
.then((result) => {
// Phone credential now linked to current user.
// User now can sign in with new phone upon logging out.
console.log(result);
})
.catch((error) => {
// Error occurred.
console.log(error);
});
}
Happy to be corrected here.
You need to get the credential from firebase.auth.PhoneAuthProvider.credential
.
// Send verification code to user's phone
firebase.auth.PhoneAuthProvider.verifyPhoneNumber(phoneNumber, recaptchVerifier).then(function(verificationId) {
// verification code from user
var cred = firebase.auth.PhoneAuthProvider.credential(verificationId, verificationCode);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With