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

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

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

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

最佳答案

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

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

在成功响应时,您可以使用 dictionary 设置用户更多详细信息,并通过使用以下代码添加 Child 来使用 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!,])

此处 user!.uid 是您主 USER 树中的唯一用户 id child 。

所以你的 firebase 树看起来像:

enter image description here

enter image description here

示例代码:

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

            }

关于ios - Firebase:将用户 UID 链接到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38097458/

相关文章:

java - firebase.auth.FirebaseUser.linkWithCredential 抛出空指针异常

android - Firebase 使用提供商帐户重置密码

ios - 将 Parse 添加到 IOS 框架会导致问题吗?

swift - 无法转换类型 '(AccountViewController) -> () -> (AccountViewController)' 的值

iphone - 应用程序更新仍然指向旧的已删除 Nib 。如何在不重新安装应用程序的情况下解决此问题?

ios - ARKit – Session 的配置检查

swift - 无法将 NSButton 添加到 Sprite-Kit 场景

swift - Xcode Swift Firebase Auth 使用电话号码和密码

ios - 为什么在 iOS 中禁用了 Google+ 登录页面的接受按钮?

ios - AVPlayer 在添加缩略图时监听