Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to update user phone number in firebase.auth (js, ts)

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 ;(

like image 993
Vlad Novikov Avatar asked Dec 24 '22 08:12

Vlad Novikov


2 Answers

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.

like image 92
HalSpace2001 Avatar answered Dec 28 '22 06:12

HalSpace2001


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);
    });
like image 33
Ti Wang Avatar answered Dec 28 '22 06:12

Ti Wang