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

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

我已经阅读了所有关于此的谷歌文档,但它似乎仍然无法正常工作。我正在设置 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
            }
        }
    })
}

感谢您提供的任何帮助,告诉我我哪里做错了,伙计们。

最佳答案

Users 下的那些键看起来不像 Firebase Auth UID。它们看起来像实时数据库推送 ID。如果它们不是实际的 UID,那么您的规则将不起作用。

关于swift - Firebase 规则 - 仅限于特定用户还是仅在获得授权的情况下?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56572710/

相关文章:

swift - 从不同的 ViewController 中删除 NSNotification Observer

ios - 在 Swift3 中缓存自定义对象数组

迁移到 android X 后 java.lang.NoClassDefFoundError 未得到解决

ios - 在 Firebase 数据更改时更新 UITableView

javascript - 无法获取 firebase 用户 ID

java - 无法在 Firebase Android 中检索 CurrentUser 的数据

ios - 如何使用 UIImageView Extension 与 Swift 异步加载图像并防止重复图像或错误图像加载到单元格中

ios - 谷歌地图 Swift 2 cocoapods

ios - 如何向 MapKit CallOuts Swift 3 添加按钮

Firebase 实时数据库。有没有办法设置记录创建的时间戳?