如何在Firebase中添加电子邮件和密码的用户名? [英] How to add username with email and password in Firebase?

查看:126
本文介绍了如何在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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆