【问题标题】:How to add username with email and password in Firebase?如何在 Firebase 中添加带有电子邮件和密码的用户名?
【发布时间】:2017-09-16 11:13:35
【问题描述】:

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

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

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() 函数添加用户名的解决方案:

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

  }
});

【问题讨论】:

标签: javascript firebase firebase-realtime-database promise firebase-authentication


【解决方案1】:

您可以创建一个users 端点并在其中存储自定义用户数据。

function writeUserData(userId, name, email, imageUrl) {
  firebase.database().ref('users/' + userId).set({
    username: name,
    email: email,
    profile_picture : imageUrl,
    // Add more stuff here
  });
}

看看https://firebase.google.com/docs/database/web/read-and-write

【讨论】:

    【解决方案2】:

    你需要自己创建那个数据库users子节点;-)

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

    要将此新用户也添加到数据库中,请尝试:

    firebase.database().ref("users").child(user.uid).set(...)
    

    【讨论】:

    • 所以我需要在 firebase.auth().createUserWithEmailAndPassword(email, password).catch(function(error){} 中添加您的代码,对吗?
    • 是的!一旦承诺返回就去做。
    【解决方案3】:

    这不能通过createUserWithEmailAndPassword() 完成,但是有一个 firebase 方法可以做到这一点。 您需要监听身份验证状态何时更改获取用户,然后更新个人资料信息。见下文

    此代码将出现在 createUserWithEmailAndPassword() 之后

     firebase.auth().onAuthStateChanged(function(user) {
    
                    if (user) {
    
                       // Updates the user attributes:
    
                      user.updateProfile({ // <-- Update Method here
    
                        displayName: "NEW USER NAME",
                        photoURL: "https://example.com/jane-q-user/profile.jpg"
    
                      }).then(function() {
    
                        // Profile updated successfully!
                        //  "NEW USER NAME"
    
                        var displayName = user.displayName;
                        // "https://example.com/jane-q-user/profile.jpg"
                        var photoURL = user.photoURL;
    
                      }, function(error) {
                        // An error happened.
                      });     
    
                    }
        });
    

    如 firebase User Api 所述:https://firebase.google.com/docs/reference/js/firebase.User#updateProfile

    希望对你有帮助

    【讨论】:

    • 有没有办法确保 displayName 在所有用户中都是唯一的?也许通过请求用户名并先查看是否有任何内容?
    猜你喜欢
    • 2021-12-26
    • 2019-11-16
    • 2019-12-25
    • 1970-01-01
    • 2021-07-25
    • 1970-01-01
    • 2021-12-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多