But users report they cannot write their own data. What is the likely problem?
medium
A. The rule is missing a semicolon at the end.
B. The rule should use request.resource.data.ownerId instead of resource.data.ownerId.
C. The rule should allow all users to write without conditions.
D. The rule should check request.auth.email instead of UID.
Solution
Step 1: Understand the difference between resource and request.resource
resource.data refers to existing data, while request.resource.data refers to new data being written.
Step 2: Identify the correct field to check ownerId
To verify the ownerId in the data being written, use request.resource.data.ownerId.
Final Answer:
The rule should use request.resource.data.ownerId instead of resource.data.ownerId. -> Option B
Quick Check:
Check new data with request.resource = The rule should use request.resource.data.ownerId instead of resource.data.ownerId. [OK]
Hint: Use request.resource for new data checks, not resource [OK]
Common Mistakes:
Confusing resource with request.resource
Ignoring the need to check new data fields
Removing conditions to fix access issues
5. You want to protect a collection so that users can only read their own documents and write only if they are the owner. Which rule correctly enforces this?
hard
A. allow read: if request.auth.uid == resource.data.ownerId; allow write: if request.auth.uid == request.resource.data.ownerId;
B. allow read: if resource.data.ownerId == request.auth.uid; allow write: if request.auth.uid == resource.data.ownerId;
C. allow read, write: if request.auth != null;
D. allow read, write: if request.auth.uid == resource.data.ownerId && request.auth.uid == request.resource.data.ownerId;
Solution
Step 1: Understand read and write conditions
Reads check existing data's ownerId with resource.data.ownerId. Writes check new data's ownerId with request.resource.data.ownerId.
Step 2: Match conditions to options
allow read: if request.auth.uid == resource.data.ownerId; allow write: if request.auth.uid == request.resource.data.ownerId; correctly separates read and write checks using the right references for existing and new data.
Final Answer:
allow read: if request.auth.uid == resource.data.ownerId; allow write: if request.auth.uid == request.resource.data.ownerId; -> Option A
Quick Check:
Read uses resource, write uses request.resource = allow read: if request.auth.uid == resource.data.ownerId; allow write: if request.auth.uid == request.resource.data.ownerId; [OK]
Hint: Read checks resource, write checks request.resource for ownerId [OK]
Common Mistakes:
Using same data reference for read and write
Allowing all authenticated users to write
Not checking ownership on writes
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.