【发布时间】: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