Firebase数据库规则.通过电话号码限制写入权限



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

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

{
"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"对象,该对象几乎包含了检查用户身份所需的所有内容。(电话号码、电子邮件、姓名等(所以我要查找的所有信息都放在"auth.token"部分。

新规则看起来像:

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

最新更新