简体   繁体   English

FIREBASE phoneAuth (LinkWithPhoneNumber)。 稍后如何在 Firebase Web 中更改用户的关联电话号码。 ( nodejs, Create-react-app )?

[英]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.我在我的 create-react-app 项目中使用了 firebase。 For SignUp purpose I am using firebaseAuth().createUserWithEmailAndPassword(email, password)出于注册目的,我使用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然后在注册后,我将他们的电话号码保存在 localStorage 中,然后使用此功能将它们重定向到 PhoneAuth 页面

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. updatePhoneNumberUser对象上有一个updatePhoneNumber方法。

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.请注意,为此您需要一个phoneCredential ,这意味着这必须是经过验证的电话号码。 See how to update user phone number in firebase.auth (js, ts) .查看如何在 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.如果您想在不验证的情况下更新用户的电话号码,可以通过 Admin SDK 完成。 For an example of this, see How to update phone number on Firebase Authentication in NodeJS?有关此示例,请参阅如何在 NodeJS 中更新 Firebase 身份验证的电话号码?

I have tried to use updatePhoneNumber API. 我试图使用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. 因此,我必须使用带有新电话号码的verifyPhoneNumber API。 Try to do this. 尝试这样做。

You need to unlink the current phone (provider.providerId === 'phone') Then you can link a new one您需要取消当前手机的链接(provider.providerId === 'phone')然后您可以链接一个新(provider.providerId === 'phone')

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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