我想检查用户名是否已被使用。这个函数被调用时,当前用户没有“用户名”的孩子:检查唯一用户名时拒绝Firebase权限
func createUsername()
{
let userID = FIRAuth.auth()?.currentUser?.uid
FIRDatabase.database().reference().updateChildValues([
"users/\(userID!)/username": username.text!,
"usernames/\(username.text!)": userID!
], withCompletionBlock: { (error, reference) in
if (error != nil) {
print("disallowed")
// Write was disallowed because username exists
}
})
}
在结合这个规则:
{
"rules": {
"users": {
"$uid": {
".read": "$uid === auth.uid",
".write": "$uid === auth.uid",
"username": {
".read": true,
".write": "!data.exists() && newData.exists()",
".validate": "newData.isString()"
}
}
},
"usernames": {
"$username": {
".read": true,
".write": "!data.exists() && newData.exists()",
".validate": "newData.isString() && newData.val() == root.child('users/' + auth.uid + '/username').val()"
}
}
}
}
我得到了答案来自:https://groups.google.com/forum/#!topic/firebase-talk/dA1Lkykd-tk 我得到这个错误消息:
[Firebase/Database][I-RDB03812] updateChildValues: at/failed: permission_denied
我不知道为什么这不会工作。我也试过这个答案:Firebase android : make username unique但是这不适合我。规则有问题,但我不知道是什么。
更新,在规则中变化不大,还是同样的错误:
{
"rules": {
"users": {
"$uid": {
".read": "$uid === auth.uid",
".write": "$uid === auth.uid",
"username": {
".read": true,
".write": "!data.exists() && newData.exists()",
".validate": "newData.isString()"
}
}
},
"usernames": {
"$username": {
".read": true,
".write": "!data.exists() && newData.exists()",
".validate": "newData.isString() && newData.val() == newData.parent().parent().child('users/' + auth.uid + '/username').val()"
}
}
}
}
我也改变了父()的东西,但它不会藏汉工作。我改为4位父母,0位父母。同样的错误。
我复制你的更新规则,并与旧的价值观取代了它。但是,我仍然遇到错误。此错误发生在.validate行,我刚刚复制的规则。在Firebase中的模拟器中,我也收到错误消息。我有这些设置:写入,位置:/用户名/ dfggdfg /,json值:{key}:“value” },验证:是,然后执行。阅读工作,但写作仍然有一些问题。其余规则与上述规定完全相同。我将更新我正在使用的当前规则。 XCode仍然给出相同的错误。 – NoKey