Reputation: 403
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.
Upvotes: 1
Views: 834
Reputation: 599021
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).
Upvotes: 2