如何在Firebase中添加电子邮件和密码的用户名? [英] How to add username with email and password in Firebase?
问题描述
我正在使用Firebase,并试图使用电子邮件和密码向数据库添加用户名。
有没有办法做到这一点,或只有 createUserWithEmailAndPassword()
函数只适用于电子邮件和密码?
$ p $ lt; code> signUp.addEventListener(click,function(user)
{
var username = usernameTxt.value;
var email = emailTxt.value;
var password = passwordTxt.value;
$ b $ firebase.auth()。createUserWithEmailAndPassword(email,password).catch(function(error)
$ b $ //处理错误
var errorCode = error.code;
var errorMessage = error.message;
如果(errorCode =='auth / email-already-in-use')
{
alert('email-already-in-use。');
}
else
{
alert(errorMessage);
}
console.log(error);
});
});
我发现如何使用createUserWithEmailAndPassword()函数添加用户名的解决方案:
$ b $
firebase.auth()。onAuthStateChanged(function(user){
var username = usernameTxt.value;
$ b $ if(user){
firebaseDataBase.ref('users /'+ user.uid).set({
email:user.email,
uid:user。 uid,
username:username
});
console.log(用户已登录);
} else {
console.log(没有用户登录);
}
});
您需要创建数据库用户
子节点自己; - )
$ b createUserWithEmailAndPassword
函数 在Firebase身份验证服务中创建新用户。数据库本身并没有因此而改变。
为了将这个新用户添加到数据库中,请尝试:
firebase.database()。ref(users)。child(user.uid).set(...)
I'm using Firebase and I'm trying to add a username to the database with the email and password.
Is there a way to do it or is createUserWithEmailAndPassword()
function only for email and password?
signUp.addEventListener("click", function(user)
{
var username = usernameTxt.value;
var email = emailTxt.value;
var password = passwordTxt.value;
firebase.auth().createUserWithEmailAndPassword(email, password).catch(function(error)
{
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
if (errorCode == 'auth/email-already-in-use')
{
alert('email-already-in-use.');
}
else
{
alert(errorMessage);
}
console.log(error);
});
});
The solution that i found to how to add username with the createUserWithEmailAndPassword() function:
firebase.auth().onAuthStateChanged(function(user) {
var username = usernameTxt.value;
if (user) {
firebaseDataBase.ref('users/' + user.uid).set({
email: user.email,
uid : user.uid,
username: username
});
console.log("User is signed in.");
} else {
console.log("No user is signed in.");
}
});
You need to create that database users
child node yourself ;-)
The createUserWithEmailAndPassword
function only creates a new user in Firebase authentication service. The database itself isn't changed at all as a result.
To add this new user to the database as well, try:
firebase.database().ref("users").child(user.uid).set(...)
这篇关于如何在Firebase中添加电子邮件和密码的用户名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!