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

时间:2016-06-02 21:11:04

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

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

我试图实现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>

4 个答案:

答案 0 :(得分:17)

为了在客户端获取用户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

返回 非null firebase.Promise包含非null firebase.User

答案 1 :(得分:6)

好像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

来访问该用户。

希望这会有所帮助:)

答案 2 :(得分:1)

createUserWithEmailAndPassword()函数返回一个所谓的Promise,它有方法catch()then()。您已经在使用catch()来处理问题。要处理&#34;非问题&#34;,您需要使用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";
  // ...
});

请参阅createUserWithEmailAndPasswordfirebase.User的参考文档。

答案 3 :(得分:0)

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