Firebase数据库保存数据

时间:2018-04-01 23:11:46

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

我目前正在使用Firebase进行电子邮件和密码身份验证。我的问题是如何为“电子邮件和密码验证部分”和“数据库”部分创建相同的UID。它们似乎有两个独立的UID。在笔记中,我使用push()作为数据库部分来保存数据。对于电子邮件和密码,我使用了简单的devdoc createemailandpassword函数。

This is the Authentication section UID

This is the Database section UID

2 个答案:

答案 0 :(得分:1)

使用createUserWithEmailAndPassword方法后,您将对用户进行身份验证,并且用户将拥有一个唯一的ID。

push()在数据库中生成随机ID。

您需要获取userid,然后将其添加到数据库中:

var user = firebase.auth().currentUser;
var uid,useremail;
if (user != null) {
uid = user.uid;
useremail = user.email;
var database = firebase.database();
database.ref().child("users").child(uid).set({
email:useremail
  });
}

更多信息:

https://firebase.google.com/docs/auth/web/manage-users

https://firebase.google.com/docs/database/web/read-and-write

答案 1 :(得分:1)

Auth面板中的UID是由Firebase在验证后生成的,当您在回调中获得此内容时,在“用户”节点下的该UID下保存“用户”字典。我不确定你使用的编码语言是什么,但在iOS(swift)中它会看起来像这样。 (这可能不完美,但你应该明白这一点。)

Auth.auth().signIn(with: credential) { (user, error) in
     //Check that there's no error and grab the user.uid property and call the below function after creating a user dictionary
})

func registerNewUserInDatabase(dictionary: Dictionary<String, Any>, uid: String) {
    userRef.child(uid).updateChildValues(dictionary)
}
相关问题