Firebase 数据库规则。按 phone 数量限制写入访问

Firebase DB rules. Limit write access by phone number

我正在使用 Firebase 实时数据库为我在不同平台上的应用程序实现同步数据库。 我需要允许每个人从数据库中读取的方法。 只有通过 phone 号码验证的用户,如果该号码在管理员列表中,才能写入数据库。我没有找到从数据库规则中的 "auth" 对象获取 phone 数字的方法。 任何帮助将不胜感激!

这是我现在使用的数据库结构和规则。

{
  "admins" : {
    "+97254000000" : {
      "name" : "Pirate Pirate",
      ...
    }
  },
  "mesages" : {
    "msg1" : {
      "orderTimestamp" : 1526916646226,
      "txt" : "some message"
    },
    "msg2" : {
      "orderTimestamp" : 1526916643522,
      "txt" : "some message"
    },
    "msg3" : {
      "orderTimestamp" : 1526916486229,
      "txt" : "some message"
    }
  }
}
// Database Rules
{
  "rules": {
    ".read": "auth != null",
    ".write": "auth != null"
  }
}

好的。经过几个小时的搜索,我停下来喝杯咖啡,马上就找到了答案:)

Firebase 数据库安全规则附带 "auth" 对象,其中包含检查用户身份所需的几乎所有内容。(phone 号码、电子邮件、姓名等) 所以我要查找的所有信息都放在 "auth.token" 部分。

新规则如下:

{
  "rules": {
    ".read": "auth != null",
    ".write": "root.child('admins').child(auth.token.phone_number).exists()"      
  }
}