【问题标题】:Firebase Returning user object after account creationFirebase在创建帐户后返回用户对象
【发布时间】:2016-10-02 19:11:05
【问题描述】:

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

我尝试实现firebase.auth().onAuthStateChangedfunction,但随后收到超时错误

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

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

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 从您链接的文档中:“成功创建用户帐户后,该用户也将登录到您的应用程序。”
    • 你是对的...我没有从旧版本中获得的更改...更正谢谢
    • 是的,这种行为已经完全改变了。我总是被它咬伤。
    • 我收到一个 auth/network-request-failed 错误实现上述。调试表明未调用 .then 承诺
    • 返回的promise变了吗?我无法访问 user.uid,而是必须执行 data.user.uid 之类的操作。
    【解决方案2】:

    createUserWithEmailAndPassword() 函数返回一个所谓的 Promise,它有方法 catch()then()。您已经在使用catch() 来处理问题。要处理“非问题”,您需要使用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 的参考文档。

    【讨论】:

    • 我收到一个 auth/network-request-failed 错误实现上述。调试表明未调用 .then 承诺
    【解决方案3】:

    看起来 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 访问用户

    希望这会有所帮助:)

    【讨论】:

    • 根据新的更改,这是正确的。谢谢,拯救了我的一天。
    【解决方案4】:
    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]
              });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多