如何在 firebase.auth (js, ts) 中更新用户电话号码

Vla*_*kov 4 javascript authentication firebase typescript firebase-authentication

如何在 firebase.auth 中更新用于身份验证的用户电话号码?

Firebase 给出方法:

updatePhoneNumber(phoneCredential)
Run Code Online (Sandbox Code Playgroud)

但是我们需要给 phoneCredential。此凭据需要对象:

  interface AuthCredential {
    providerId: string;
    signInMethod: string;
  }
Run Code Online (Sandbox Code Playgroud)

这个手机凭据中必须有什么,我尝试发送

providerId: 'phone';
signInMethod: 'phone';
Run Code Online (Sandbox Code Playgroud)

这是行不通的;(

小智 5

使用当前用户/登录帐户更新电话号码的解决方案。万一这对任何人都有帮助。

问题:假设您有一个当前登录的帐户,其中包含任意数量的单个/合并帐户,并且他们想要更新其编号。他们会怎么做。

解决方案:以下是 Web/JS 的 Auth 方法,您的目标平台将存在等效的方法。

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);
        });
}
Run Code Online (Sandbox Code Playgroud)

很高兴在这里得到纠正。


小智 3

您需要从 获取凭证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);
    });
Run Code Online (Sandbox Code Playgroud)