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

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

问题描述

我试图在 Firebase 中创建一个用户,然后在网络服务器上的数据库中创建一个用户配置文件.我已经实现了以下代码,它可以很好地创建用户.但是我不确定如何接收用户 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/参考/js/firebase.auth.Auth#createUserWithEmailAndPassword

退货非空 firebase.Promise 包含非空 firebase.User

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

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

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