ios - Firebase: linking user UID to database -
i'm trying use current users uid unique identifier store associated user data there user uid in realtime database i.e. ref.child("user/uid"):
example: when user creates account added authentication section of firebase console , lets user fills in form i.e first name, last name, dob etc. how link data user uid in realtime database.
when register new user need first register code:
firauth.auth()?.createuserwithemail(useremail, password: userpassword, completion: { (user, error) in
on success response can set user more details use dictionary
, use setvalue
method adding child using following code:
ref.child("users").child(user!.uid).setvalue(["provider": self.txtpassword.text!,"email": self.txtemail.text!,"firstname": self.txtfname.text!,"lastname": self.txtlname.text!,"phone": self.txtphone.text!,])
here user!.uid unique user id child in main user tree.
so firebase tree :
and
sample code:
@ibaction func actionregister(sender: uibutton) { let ref : firdatabasereference! ref = firdatabase.database().reference() firauth.auth()?.createuserwithemail("abc@abc.com", password: "1234567", completion: { (user, error) in if((error) != nil) { print("error = \(error?.localizeddescription)") } else { // following method add user's more details ref.child("users").child(user!.uid).setvalue(["provider": "1234566", "email": "abc@abc.com", "firstname": "nitin", "lastname": "gohel", "phone": "12345678" ]) //following method registers user details ref.child("users").child(user!.uid).observesingleeventoftype(.value, withblock: { snapshot in let postdict = snapshot.value as! [string : anyobject] print("error = \(postdict)") }) } }) }
Comments
Post a Comment