Firebase 在创建帐户后返回用户对象

Firebase Returning user object after account creation

我正在尝试在 Firebase 中创建用户,然后在 Web 服务器上的数据库中创建用户配置文件。我已经实现了以下代码,可以很好地创建用户。但是我不确定如何接收用户 ID(我需要一个唯一 ID)来创建数据库结构。有没有办法在调用 createUserWithEmailAndPassword 时 return 用户对象?

我试图实现一个 firebase.auth().onAuthStateChanged 函数,但随后收到超时错误

如果你还没有收集到这是一个网络应用程序。

<script>
function createUser() {
var Result = "true";
var textUser = document.getElementById('userName').value;
var textPassword = document.getElementById('userPassword').value;
var textAccountID = document.getElementById('accountRef').value;
var textDateCreated = document.getElementById('dateCreated').value;
var textDisplayName = document.getElementById('displayName').value;
var UID;

firebase.auth().createUserWithEmailAndPassword(textUser, textPassword).catch(function(error) {
  // Handle Errors here.
  var errorCode = error.code;
  var errorMessage = error.message;
  return Result = "false";
  // ...
});

writeUserData(UID, textDisplayName, textAccountID, textDateCreated);

return Result;

}

function writeUserData(userId, displayName, accountID, dateCreated) {
  firebase.database().ref('User/' + userId).set({
  userId:{
    AccountID: accountID,
    Created: dateCreated,
    Name: displayName}
  });
}


</script>

为了在客户端获取用户 ID,您应该这样做:

firebase.auth().createUserWithEmailAndPassword(textUser, textPassword)
.then(function(user){
  console.log('uid',user.uid)

  //Here if you want you can sign in the user
}).catch(function(error) {
    //Handle error
});

如此处所述:

https://firebase.google.com/docs/reference/js/firebase.auth.Auth#createUserWithEmailAndPassword

Returns 非空 firebase.Promise 包含非空 firebase.User

createUserWithEmailAndPassword()函数returns一个所谓的Promise,它有方法catch()then()。您已经在使用 catch() 来处理问题。要处理 "non-problems",您需要使用 then():

firebase.auth().createUserWithEmailAndPassword(textUser, textPassword)
.then(function(user) {
  console.log(user); // see https://firebase.google.com/docs/reference/js/firebase.User
})
.catch(function(error) {
  // Handle Errors here.
  var errorCode = error.code;
  var errorMessage = error.message;
  return Result = "false";
  // ...
});

请参阅 createUserWithEmailAndPassword and for firebase.User 的参考文档。

看来 firebase 对此方法做了一些更改。接受的解决方案需要一点修正:

firebase.auth().createUserWithEmailAndPassword(textUser, textPassword)
.then(function(data){
  console.log('uid',data.user.uid)

  //Here if you want you can sign in the user
}).catch(function(error) {
    //Handle error
});

现在您可以通过 data.user

访问用户

希望对您有所帮助:)

firebase.auth().createUserWithEmailAndPassword(email.value, password.value).then(function (user) {
            //var user = firebase.auth().currentUser;
            alert('Cadastro feito com sucesso!', "success");
            console.log("New user Id: "+ user.uid);

          }).catch(function (error) {
            // Handle Errors here.
            var errorCode = error.code;
            var errorMessage = error.message;
            // [START_EXCLUDE]
            if (errorCode == 'auth/weak-password') {
              alert('A senha é muito fraca.', "warning");
              //alert('A senha é muito fraca.');
            } else if (errorMessage == "The email address is already in use by another account.") {
                errorMsg = "Esse email já está sendo usado por outra conta";
                alert(errorMsg, "danger");

              //alert(errorMessage);
            }else{
              alert(errorMessage);
            }
            
            // [END_EXCLUDE]
          });