模拟器在Firebase中未显示任何错误,但权限被拒绝(Simulator shows no error in Firebase, but the permission is denied)

这些是我的规则:

{ "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()" } } } }

但是,在模拟器中我看到一个错误,但不是错误发生的那一行。 我只看到“写拒绝”。 在XCode中的模拟器中执行此操作时,我看到“权限被拒绝”。 我想检查给定的用户名是否唯一,即全部。

编辑:

也许我不太清楚。 模拟器确实显示错误,但显示错误。 然而这是我的错误。 见图1: 我在路上犯了一个错误。

这是我的第二次尝试,图2:

在图3中,我添加了错误描述:

这在模拟器中出错了,我可以从我的项目中添加代码,但我认为规则并不好。

These are my rules:

{ "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()" } } } }

However, in the simulator I see an error, but not what line the error occurs. I just see "write denied". When executing this in the simulator in XCode, I see "permission denied". I want to check if the given username is unique, that is all.

Edit:

Maybe I was not so clear. The simulator does shows an error, but not at which line. This was however my mistake. See picture 1: I made a mistake at the path.

This is my second attempt, picture 2:

At picture 3 I added the error description:

This goes wrong in the simulator, I can add the code from my project but I think the rules are not good.

最满意答案

我将依次处理截图:

在屏幕截图1中,您正在尝试写入/usernames 。 没有人对/usernames具有写权限(仅在其下的特定节点上),因此写入失败。

在屏幕截图2中,您正在写入/usernames/myusername 。 但是您编写的数据违反了验证规则。 您要编写的是JSON对象,而您的规则只允许使用简单的字符串值。 所以你可以写"userUID" ,但你不能写{ "hello": "userUID" } 。

在屏幕截图3中,您可以看到您具有写入权限,如.write规则旁边的绿色复选标记所示。 但.validate规则拒绝写入,因为它正在检查字符串(并且您正在发布JSON对象)。

I'll treat the screenshots in turn:

In screenshot 1, you're trying to write to /usernames. Nobody has write permission on /usernames (only on specific nodes under it) so the write fails.

In screenshot 2, you're writing to /usernames/myusername. But the data you're writing violates the validation rule. What you're trying to write is a JSON object, while your rule only allows a simple string value. So you could write "userUID", but you can't write { "hello": "userUID" }.

In screenshot 3 you can see that your have write permission on that, as shown by the green checkmark next to the .write rules. But the .validate rule is rejecting the write, because it's checking for a string (and you're posting a JSON object).

更多推荐