首页 > 解决方案 > Firebase 当前用户更新电话号码

问题描述

我正在使用firebase Auth,我有一个包含两个字段名称和电话号码以及onsubmit 方法的表单,我想更新当前登录用户的电话号码和姓名。但它不会在提交时更新电话号码。仅成功更新用户的显示名。请检查下面的代码。onUpdateProfile 是表单提交函数。

<template>
  <form @submit.prevent="onUpdateProfile">
    <input type="text" v-model="profile.name" placeholder="Enter Your Name..." />
    <input type="text" v-model="profile.phonenumber" placeholder="Enter Your phone..." />
    <button  type="submit">submit</button>
  </form>
</template>

    methods: {
      onUpdateProfile() {
              firebase.auth().onAuthStateChanged(user => {
                if (user) {
                  //Getting Current User
                  let user = firebase.auth().currentUser;
                  user
                    .updateProfile({
                      displayName: this.profile.name,
                      phoneNumber: this.profile.phoneNumber
                    })
                    .then(() => {console.log('success})
                    .catch(error => {console.log(error});
                  //Updating User other details on submit
                } else {
                }
              });
            }
    }


    data() {
        return {
          profile: {
            name: "",
            phonenumber: ""
          }
        };
      }

标签: javascriptfirebasefirebase-authentication

解决方案


您不能用于updateProfile更新phoneNumber. 您必须使用updatePhoneNumberAPI,因为 Firebase 身份验证始终需要先验证电话号码,然后再将其保存给用户。

这类似于signInWithPhoneNumber.

const phoneNumber = getPhoneNumberFromUserInput();
const appVerifier = new firebase.auth.RecaptchaVerifier(...);
firebase.auth().currentUser.updatePhoneNumber(phoneNumber, appVerifier)
  .then((confirmationResult) => {
    // SMS sent. Prompt user to type the code from the message, then complete
    // verification by calling confirmationResult.confirm(code).
    ...
    return confirmationResult.confirm(smsCode);
  }).then((userCredential) => {
    // Phone set on the user.
  }).catch((error) => {
    // Error occurred.
  });

推荐阅读