Consider this Firebase Realtime Database rule snippet:
{
"rules": {
"messages": {
"$messageId": {
".read": "auth != null",
".write": "auth != null && auth.uid == data.child('owner').val()"
}
}
}
}What happens when a logged-in user tries to write a message where the 'owner' field is not equal to their user ID?
Check the condition for '.write' and what 'data.child('owner').val()' means.
The rule requires the user to be authenticated and their user ID to match the 'owner' field of the existing message. If the 'owner' does not match, the write is denied.
In Firebase Realtime Database security rules, what does the auth variable represent?
Think about how Firebase identifies who is making a request.
The auth variable contains the user's authentication info if logged in, or null if not.
Given this Firebase rule:
{
"rules": {
"users": {
"$uid": {
".write": "auth != null && auth.uid == $uid"
}
}
}
}Which of the following actions is not allowed by this rule?
Check the condition comparing auth.uid and $uid.
The rule only allows writes when the authenticated user's ID matches the user ID in the path. Writing to another user's data is denied.
You want to allow anyone to read data under /posts but only authenticated users to write. Which rule configuration achieves this?
Remember true means anyone can read.
Setting .read to true allows public read access. Setting .write to auth != null restricts writes to authenticated users.
You have a Firebase Realtime Database with a /projects node. Each project has an ownerId and a list of collaborators (user IDs). You want to allow:
- Owners to read and write their projects.
- Collaborators to read but not write.
- Others no access.
Which rule snippet correctly implements this?
Think about who can read and who can write, and how to check collaborators.
Option A allows read access to owners and collaborators, write access only to owners. Others have no access. Other options either allow too much or too little access.