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

查看:18
本文介绍了如何在 Firebase 中添加带有电子邮件和密码的用户名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Firebase,我正在尝试使用电子邮件和密码将用户名添加到数据库中.

I'm using Firebase and I'm trying to add a username to the database with the email and password.

有没有办法做到这一点,还是 createUserWithEmailAndPassword() 函数仅适用于电子邮件和密码?

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);

    });
});

我发现如何使用 createUserWithEmailAndPassword() 函数添加用户名的解决方案:

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.");

  }
});

推荐答案

您需要自己创建该数据库 users 子节点 ;-)

You need to create that database users child node yourself ;-)

createUserWithEmailAndPassword 函数在 Firebase 身份验证服务中创建一个新用户.因此,数据库本身根本没有改变.

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天全站免登陆