Firebase为当前用户

时间:2016-03-22 17:22:20

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

我试图让授权的当前用户读取和写入他自己的数据到Firebase数据库。我的安全数据如下所示:

{
  "features" : {
    "-KDUBJIPwvLCK-1lV4bM" : {
      "e1fd0ccb-370a-4a6f-86af-87fdf97d25a0" : "een"
    },
    "-KDUBLUr6K_PMxWkgcof" : {
      "e1fd0ccb-370a-4a6f-86af-87fdf97d25a0" : "twee"
    }
  },
  "users" : {
    "e1fd0ccb-370a-4a6f-86af-87fdf97d25a0" : {
      "email" : "tjalling@horecalife.nl",
      "image" : "iVBORw0KGgoAAAANSUhEUgAAASwAAAEsCAYAAAB5fY51AAAAAXNSR0IArs4c6QAA\r\nABxpRE9UAAAA",
      "name" : "tjalling dijkstra",
      "telephone number" : "06580055"
    }
  }
}

-KDUBJIPwvLCK-1lV4bM是从childByAutoId()方法获得的autoId

e1fd0ccb-370a-4a6f-86af-87fdf97d25a0是一个uid(唯一用户ID)。

een是某个功能的价值。

我的安全规则是:

{
  "rules": {
    "users": {
      "$uid": {
        ".read": "auth != null && auth.uid == $uid",
        ".write": "auth != null && auth.uid == $uid"
      }
    },
    "features": {
      "$autoId" : { 
      "$uid": {
        ".read": "auth != null && auth.uid == $uid",
        ".write": "auth != null && auth.uid == $uid"
      }
    }
  }
}
}

如果我写这样的Firebase规则,则功能部分会拒绝setValue()的权限。

  

2016-03-22 17:51:27.077 Herodus [884:228445] [Firebase] setValue:或removeValue:at / features / -KDUEEZ4z-FCAoUci-rW failed:permission_denied。

此代码段添加了一项新功能:

let featureContents = addFeatureTextField.text 
if featureContents != "" { 
  let newFeature: String = featureContents! 
  let featureDict = [ uid: newFeature] 
  let ref = DataService.dataService.FEATURE_REF.childByAutoId()
  ref.setValue(featureDict)

有人,任何线索我在这里做错了什么?

1 个答案:

答案 0 :(得分:1)

你正在写这条道路:

/features/-KDUEEZ4z-FCAoUci-rW 

您的规则仅在此路径上提供写入权限:

/features/$autoId/$uid

您希望使用以下内容编写数据:

ref.childByAppendingPath(authData.uid).setValue(featureDict)