【问题标题】:Firebase: linking user UID to databaseFirebase:将用户 UID 链接到数据库
【发布时间】:2016-11-01 00:50:44
【问题描述】:

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

示例:当用户创建一个帐户时,将其添加到 Firebase 控制台的身份验证部分,并假设用户填写了一个表单,即名字、姓氏、出生日期等。我如何将数据链接到用户 UID在实时数据库中。

【问题讨论】:

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


    【解决方案1】:

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

     FIRAuth.auth()?.createUserWithEmail(UserEmail, password: UserPassword, completion: { (user, error) in
    

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

    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!,])
    

    这里 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)")
                            })    
                        }
                    })
    
                }
    

    【讨论】:

    • 我附上了示例代码,要了解更多信息,您需要学习其文档firebase.google.com/docs/database/ios/save-data#basic_write
    • 谢谢,是否可以在示例代码的添加更多细节部分使用文本字段?
    • 是否也可以在没有帐户创建部分的情况下将更多数据存储到用户 uid 但在不同的视图控制器上?谢谢
    猜你喜欢
    • 2017-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 2021-02-09
    • 2017-07-30
    相关资源
    最近更新 更多