[英]How to add user's extra information such as display name in firebase using javascript?
Using createUserWithEmailAndPassword
function of firebase I am able to sign users up but how to add extra information such as display name and picture URL?使用 firebase 的createUserWithEmailAndPassword
function 我可以注册用户,但如何添加额外信息,例如显示名称和图片 ZE6B391A8D2C4D45902A23A8B6585703D This is what I have done.这就是我所做的。
const signup = document.querySelector('#signup-form');
signup.addEventListener('submit', e=>{
e.preventDefault();
//get user info
const first_name = signup['firstname'].value;
const last_name = signup['lastname'].value;
const email = signup['email1'].value;
const password = signup['pswd1'].value;
//sigup the user
firebase.auth().createUserWithEmailAndPassword(email, password).then(()=>{
firebase.auth().onAuthStateChanged(function(user){
if(user){
user.updateProfile({
displayName: first_name
})
}
})
signup.reset();
}).catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
window.alert("Error: " + errorMessage);
// ...
});
});
Once you have created the user, you can access user object provided by firebase.创建用户后,即可访问firebase提供的用户object。 Using its updateProfile function, you can pass an object containing the properties you want the user object to have, like the code below:使用它的 updateProfile function,您可以传递包含您希望用户 object 拥有的属性的 object,如下面的代码:
var user = firebase.auth().currentUser;
user.updateProfile({
displayName: "Jane Q. User",
photoURL: "https://example.com/jane-q-user/profile.jpg"
}).then(function() {
// Update successful.
}).catch(function(error) {
// An error happened.
});
This worked for me.这对我有用。
firebase.auth().createUserWithEmailAndPassword(email, password).then(cred=>{
cred.user.updateProfile({
displayName: first_name + " " + last_name
})
})
.catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
window.alert("Error: " + errorMessage);
// ...
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.