admin管理员组文章数量:1415092
I am using firebase Auth and I have a form with two fields name and phone number and onsubmit method and I want to update currently logged in user's phone number and name. But its not updating the phone number on submit. Only successfully updating the user's displayname. Please check the code below. onUpdateProfile is the form submit function.
<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: ""
}
};
}
I am using firebase Auth and I have a form with two fields name and phone number and onsubmit method and I want to update currently logged in user's phone number and name. But its not updating the phone number on submit. Only successfully updating the user's displayname. Please check the code below. onUpdateProfile is the form submit function.
<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: ""
}
};
}
Share
Improve this question
edited Dec 9, 2019 at 16:41
RAHUL KUNDU
asked Dec 9, 2019 at 16:02
RAHUL KUNDURAHUL KUNDU
8932 gold badges16 silver badges39 bronze badges
3
- What's the problem when you run this code? – Frank van Puffelen Commented Dec 9, 2019 at 16:04
- I want to update the phone number on submit but it's not updating. I tried to log the phone number in the console it's returning null – RAHUL KUNDU Commented Dec 9, 2019 at 16:36
-
You can't change the phone number through a call to
updateProfile
. Instead you should callupdatePhoneNumber
. See firebase.google./docs/reference/js/… – Frank van Puffelen Commented Dec 9, 2019 at 17:02
3 Answers
Reset to default 2You can't use updateProfile
to update the phoneNumber
. You have to use the updatePhoneNumber
API as Firebase Auth always requires the phone number to be verified before saving it on a user.
This is similar to 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 plete
// verification by calling confirmationResult.confirm(code).
...
return confirmationResult.confirm(smsCode);
}).then((userCredential) => {
// Phone set on the user.
}).catch((error) => {
// Error occurred.
});
UPDATE
According to the Firebase documentation, it looks like you can only update phone number if you are using firebase-admin. Without it you can only update basic information, such as the user's display name and profile photo URL.
If I am wrong please feel free to correct me.
https://firebase.google./docs/auth/admin/manage-users#update_a_user
https://firebase.google./docs/auth/web/manage-users#update_a_users_profile
you are using different names;
in template you have this:
v-model="profile.phonenumber"
and in the onUpdateProfile() method
phoneNumber: this.profile.phoneNumber
This worked for me:
async function save(phone: string, e) {
e.preventDefault();
const { currentUser:fuser } = firebase.auth();
if(fuser && fuser.phoneNumber !== phone) {
try {
const verifier = new firebase.auth.RecaptchaVerifier('recaptcha-container', {
callback: (response) => console.log('callback', response),
size: 'invisible',
});
const phoneProvider = new firebase.auth.PhoneAuthProvider();
const id = await phoneProvider.verifyPhoneNumber(phone, verifier);
const code = window.prompt('Bitte zugeschickten Code eingeben');
const cred = firebase.auth.PhoneAuthProvider.credential(id, code);
await fuser.updatePhoneNumber(cred);
console.log('phone number changed', id, cred, fuser);
setSuccess(true);
} catch(e) {
console.error(e);
}
}
}
本文标签: javascriptFirebase current user update Phone NumberStack Overflow
版权声明:本文标题:javascript - Firebase current user update Phone Number - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745158631a2645325.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论