Firebase规则。仅验证某些字段

时间:2018-03-26 09:17:57

标签: firebase web firebase-realtime-database

我想只为下一个结构访问我的firebase“write”: My structure

这个想法是以这样的方式禁止数据写作:

用户 - > 电子邮件 电话 用户名

1 个答案:

答案 0 :(得分:1)

如果您希望用户只能使用电子邮件,电话和用户名字段,则可以使用与此类似的规则:

{
  "rules": {
    "Users": {
        "$user_id": {
            //Every authenticated user can read
            ".read": "auth != null ",
            // grants write access to the owner of this user account
            // whose uid must exactly match the key ($user_id)
            ".write": "$user_id === auth.uid",
            //This line says the new data must have ATLEAST these children
            ".validate": "newData.hasChildren(['email','phone', 'username'])",   
            //You can add individual validation for email, phone and username here     
            "email": { ".validate": true },
            "phone": { ".validate": true },
            "username": { ".validate": true },
            //This rule prevents validation of data with more child than defined in the 2 lines above (or more if you specify more children)
            "$other": { ".validate": false }
        }
    }
  }
}

有关验证数据的详细信息,请查看the firebase security docs