【问题标题】:Firebase Validate Rule not workingFirebase 验证规则不起作用
【发布时间】:2018-03-12 10:53:10
【问题描述】:

我不熟悉使用 Firebase 规则,但我已通读文档。我知道最重要的规则优先,但验证规则仍应运行。我的问题是我正在尝试创建唯一的用户名,但是每当我模拟插入时,验证永远不会运行。我要创建的验证规则应该检查用户名的集合并查看它是否已经存在,如果存在则写入应该失败。 我在下面发布了我的数据结构和规则。 fire_uid 是 fire base 中生成的 uid 的占位符

当前数据库

{
  "userProfile" : {
    "fire_uid" : {
      "email" : "m@matt.com",
      "name" : "matt maxton"
    },
    "fire_uid" : {
      "email" : "v@victor.com",
      "name" : "victor tictor"
    }
  },
  "usernames" : {
    "testcaseusername" : "fire_uid"
  }
}

Firebase 规则

{
    "rules": {
        "$uid": {
            ".write": "auth !== null",
            ".read": "auth !== null && auth.provider === 'password'",
            "usernames": {
                ".validate": "!root.child('usernames').child(newData.val()).exists() ||root.child('usernames').child(newData.val()).val() == $uid "
            }
        }
    }
}

模拟测试(我确保这个测试中的用户id与在数据库中用于创建条目的uid不同

位置:/用户名

Json 数据:{“testcaseusername”:“fire_uid”}

【问题讨论】:

    标签: validation firebase firebase-realtime-database firebase-security


    【解决方案1】:

    您的验证规则位于/$uid/usernames。但是你的 JSON 有 /userProfile/$uid/name。要验证后者中的用户名,请使用:

    {
        "rules": {
          "userProfile":
            "$uid": {
                ".write": "auth !== null",
                ".read": "auth !== null && auth.provider === 'password'",
                "name": {
                    ".validate": "!root.child('usernames').child(newData.val()).exists()
                                 ||root.child('usernames').child(newData.val()).val() == $uid "
                }
            }
          }
        }
    }
    

    【讨论】:

    • 'usernames' 元素位于数据库根目录,它与层次结构中的 'userprofile' 处于同一级别
    • 我想你可能已经给我指出了正确的方向,验证规则只在 $uid/usernames 上检查,我需要在 /usernames 上检查它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-27
    • 2013-12-01
    • 1970-01-01
    • 2015-04-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多