Firebase规则-仅限于特定用户或仅在获得授权的情况下?

时间:2019-06-13 02:45:42

标签: swift firebase firebase-realtime-database firebase-security-rules

我已经阅读了所有的google文档,但似乎仍然无法正常工作。我正在设置Firebase规则,当我使用以下命令时,我可以通过用户身份验证相应地进行读写:

{
  "rules": {
    "Users": { 
        ".write": "auth.uid != null",
        ".read": "auth.uid != null"
            },
     },
}

但是,当我使用以下命令时,我的权限被拒绝:

{
  "rules": {
    "Users": { 
      "$uid":{
        ".write": "auth.uid ===  $uid",
        ".read": "auth.uid === $uid"
        },
     },
   },
}

这是我的数据库:

Users
 -tnRYoVw1duUKV30VgtOxUTvb5kb2
   FirstName: "Mike"
   LastName: "Smith"
   myuser: "msmith"
 -saUXlTt5ioLNB33Iii9rHCdq4ov6
   FirstName: "John"
   LastName: "Heart"
   myuser: "jheart"

这是来自swift的读取查询:

override func viewDidAppear(_ animated: Bool) {
    ref = Database.database().reference()
    ref?.child("Users").queryOrdered(byChild: "myuser").queryEqual(toValue: emailtextfield.text).observe(.value, with: { (snapShot) in
        if (snapShot.value! is NSNull) {
            print("nothing found")
        } else {
            let snapShotValue = snapShot.value as! [String:[String:Any]]
            Array(snapShotValue.values).forEach {
                let theFirstName = $0["FirstName"] as! String
                let theLastName = $0["LastName"] as! String
                self.firstName.text = theFirstName
                self.lastName.text = theLastName
            }
        }
    })
}

感谢您提供的帮助,告诉我我哪里做错了。

1 个答案:

答案 0 :(得分:2)

“用户”正下方的那些键看起来不像Firebase Auth UID。它们看起来像实时数据库推送ID。如果它们不是实际的UID,则您的规则将行不通。