简体   繁体   中英

Firebase Database Saving Data

I am currently using Firebase for email and password Authentication. My problem is how do I create the same UID for the 'Email and password authentication section' and the 'Database' section. They appear to have two separate UID. On a note, I used push() for the database section to save the data. For the email and password I used the simple devdoc createemailandpassword function.

这是身份验证部分 UID

这是数据库部分 UID

After using createUserWithEmailAndPassword method, you will authenticate the user and, the user will have an unique id for him.

The push() generates a random id in the database.

You need to get the userid and then add it to the database:

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
  });
}

more info here:

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

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

The UID in the Auth panel is generated by Firebase after authentication, when you get this in the callback, save a "user" dictionary under that UID, under a "Users" node. I'm not sure what coding language you're using but in iOS (swift) it would look something like this. (It may not be perfect but you should get the idea).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM