如何在firebase.auth(js,ts)中更新用户电话号码 [英] how to update user phone number in firebase.auth (js, ts)

查看:80
本文介绍了如何在firebase.auth(js,ts)中更新用户电话号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can i update user phone number which it use for auth in firebase.auth??

Firebase给出为方法:

Firebase give as method:

updatePhoneNumber(phoneCredential)

但是我们需要提供 phoneCredential . 该凭证具有对象:

But we need give phoneCredential. This credential takes object:

  interface AuthCredential {
    providerId: string;
    signInMethod: string;
  }

phoneCredential 中必须包含什么,我尝试发送

What must be in this phoneCredential, i try to send

providerId: 'phone';
signInMethod: 'phone';

这是行不通的;(

推荐答案

使用当前用户/登录帐户来更新PhoneNumber的解决方案.万一这对任何人都有帮助.

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.

解决方案: 以下是Auth for Web/JS方法,等效于您的目标平台.

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.

这篇关于如何在firebase.auth(js,ts)中更新用户电话号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆