创建帐户后,Firebase返回用户对象 [英] Firebase Returning user object after account creation

查看:58
本文介绍了创建帐户后,Firebase返回用户对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Im trying to create a user within Firebase and then create a user profile within the database on a web server. I have implemented the following code which creates the user quite nicely. However im not sure on how to receive the user id (which i need for a unique ID) to create the database structure. Is there a way to return a user object when the createUserWithEmailAndPassword is called?

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

I have tried to implement a firebase.auth().onAuthStateChangedfunction but i then receive a timeout error

如果您还没有收集到,则用于网络应用程序.

If you havn't gathered this is for a web app.

<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,您应该执行以下操作:

In order to get the user id in the client side you should do this:

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

Returns non-null firebase.Promise containing non-null firebase.User

这篇关于创建帐户后,Firebase返回用户对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆