Javascript Firebase当前用户更新电话号码
我正在使用firebase Auth,我有一个带有两个字段名称和电话号码以及onsubmit方法的表单,我想更新当前登录用户的电话号码和名称。但它不会在提交时更新电话号码。仅成功更新用户的displayname。请检查下面的代码。onUpdate配置文件是表单提交功能Javascript Firebase当前用户更新电话号码,javascript,firebase,firebase-authentication,Javascript,Firebase,Firebase Authentication,我正在使用firebase Auth,我有一个带有两个字段名称和电话号码以及onsubmit方法的表单,我想更新当前登录用户的电话号码和名称。但它不会在提交时更新电话号码。仅成功更新用户的displayname。请检查下面的代码。onUpdate配置文件是表单提交功能 <template> <form @submit.prevent="onUpdateProfile"> <input type="text" v-model="profile.name"
<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: ""
}
};
}
提交
方法:{
onUpdateProfile(){
firebase.auth().onAuthStateChanged(用户=>{
如果(用户){
//获取当前用户
让user=firebase.auth().currentUser;
用户
.updateProfile({
displayName:this.profile.name,
phoneNumber:this.profile.phoneNumber
})
.then(()=>{console.log('success})
.catch(错误=>{console.log(错误});
//正在更新提交时的用户其他详细信息
}否则{
}
});
}
}
数据(){
返回{
简介:{
姓名:“,
电话号码:“
}
};
}
更新
根据Firebase文档,似乎只有在使用Firebase admin时才能更新电话号码。没有它,您只能更新基本信息,例如用户的显示名称和个人资料照片URL
如果我错了,请随时纠正我
你使用不同的名字 在模板中,您有以下内容:
v-model="profile.phonenumber"
在onUpdateProfile()方法中
您不能使用
updateProfile
来更新phoneNumber
。您必须使用updatePhoneNumber
API,因为Firebase Auth始终要求在将电话号码保存到用户上之前对其进行验证
这类似于带有电话号码的签名
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.
});
这对我很有用:
异步函数保存(电话:字符串,e){
e、 预防默认值();
const{currentUser:fuser}=firebase.auth();
if(fuser&&fuser.phoneNumber!==电话){
试一试{
const-verifier=new-firebase.auth.repactchaverifier('repactcha-container'{
回调:(响应)=>console.log('callback',response),
大小:'不可见',
});
const phoneProvider=new firebase.auth.PhoneAuthProvider();
const id=wait phoneProvider.verifyPhoneNumber(电话,验证器);
const code=window.prompt('bite zugeschickten code eingelen');
const cred=firebase.auth.PhoneAuthProvider.credential(id、代码);
等待fuser.updatePhoneNumber(cred);
console.log('电话号码已更改',id,cred,fuser);
设置成功(true);
}捕获(e){
控制台错误(e);
}
}
}
运行此代码时出现了什么问题?我想在提交时更新电话号码,但它没有更新。我试图将电话号码记录在控制台中,但它返回null。您不能通过调用更新配置文件来更改电话号码。相反,您应该调用更新电话号码
。更改名称后请参阅提交后的仍然返回phoneNumber:null您是否尝试在控制台上打印profile.phoneNumber?它是来自表单的null还是存在值,但updateProfile()方法有问题?您是否可以共享正确的updateProfile()方法我已注销控制台中返回null的phoneNumber
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.
});