Firebase:将用户UID链接到数据库

cri*_*k12 2 ios firebase swift firebase-authentication firebase-realtime-database

我正在尝试使用当前用户uid作为唯一标识符来存储关联的用户数据,因为实时数据库中存在用户uid,即ref.child("User/uid"):

例如:当用户创建其添加到火力地堡控制台的认证部的帐户,并让说,用户在表单即姓,名,出生日期等填充如何将我的实时数据链接到用户的UID数据库.

Nit*_*hel 6

当您注册任何新用户时,您需要先注册代码:

 FIRAuth.auth()?.createUserWithEmail(UserEmail, password: UserPassword, completion: { (user, error) in
Run Code Online (Sandbox Code Playgroud)

在成功响应上,您可以使用以下代码添加Child,为用户设置更多详细信息使用dictionary和使用SetValue方法:

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!,])
Run Code Online (Sandbox Code Playgroud)

这里user!.uid是主USER树中唯一的用户ID子项.

所以你的firebase树看起来像:

在此输入图像描述

和

在此输入图像描述

示例代码:

    @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 is = \(error?.localizedDescription)")
                    }
                    else
                    {

          // following method is a 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 get the registers user details 
         ref.child("users").child(user!.uid).observeSingleEventOfType(.Value, withBlock: { snapshot in
                            let postDict = snapshot.value as! [String : AnyObject]
                            print("Error is = \(postDict)")
                        })    
                    }
                })

            }
Run Code Online (Sandbox Code Playgroud)