我尝试在 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>
为了在客户端获取用户 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.User 的非空 firebase.Promise
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)