简体   繁体   中英

FIREBASE phoneAuth ( LinkWithPhoneNumber ) . How to change a user's linked phone number later in Firebase Web. ( nodejs, Create-react-app )?

I am using firebase in my create-react-app project. For SignUp purpose I am using firebaseAuth().createUserWithEmailAndPassword(email, password)

Then after SignUp I am saving their phoneNumber in localStorage and redirect them to PhoneAuth page using then I am using this function

export function PhnAuth(phone) {
    window.recaptchaVerifier = new firebaseAuth.RecaptchaVerifier('recaptcha-container',{'size': 'small'});
    return firebaseAuth().currentUser.linkWithPhoneNumber(phone, window.recaptchaVerifier)
    .then(function (confirmationResult) {
        window.confirmationResult = confirmationResult;
      }).catch(function (error) {
    })
}

After the recaptcha and all done I get the I successfully linked the user's email with their phoneNumber. But how to update that phoneNumber later ? I couldn't find anything regarding updating a linked phoneNumber in the docs.

There's an updatePhoneNumber method on the User object for that purpose.

See the reference docs and the documentation on updating a user's profile .

Note that you'll need a phoneCredential for this, meaning that this must be a verified phone number. See how to update user phone number in firebase.auth (js, ts) .

If you want to update a user's phone number without verifying it, that can be done from the Admin SDK. For an example of this, see How to update phone number on Firebase Authentication in NodeJS?

I have tried to use updatePhoneNumber API. To get phone credential, I think you must get verification code with new phone number. So I have to use verifyPhoneNumber API with new phone number. Try to do this.

You need to unlink the current phone (provider.providerId === 'phone') Then you can link a new one

    const currentUser = firebaseAuth().currentUser;

    currentUser.unlink('phone').then(successCallback).catch(errorCallback)

To check if the phone is linked to the current user you need to check the list of providers

    const phoneProviders = currentUser.providerData.filter(
      provider => provider.providerId === 'phone'
    );
    if (phoneProviders.length > 0) {
      currentUser.unlink('phone').then(successCallback).catch(errorCallback);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM