Resource and request objects in Firebase - Commands & Configuration
rules_version = '2'; service cloud.firestore { match /databases/{database}/documents { match /users/{userId} { allow read, write: if request.auth != null && request.auth.uid == userId; } match /posts/{postId} { allow read: if true; allow write: if request.auth != null && request.resource.data.keys().hasAll(['title', 'content']) && request.resource.data.title is string && request.resource.data.content is string; } } }
This file defines rules for Firestore database access.
rules_version: sets the version of rules syntax.
service cloud.firestore: targets Firestore database.
match /databases/{database}/documents: applies rules to all documents.
match /users/{userId}: allows only authenticated users to read/write their own user data.
match /posts/{postId}: allows anyone to read posts but only authenticated users can write posts with required fields 'title' and 'content' as strings.
request.auth: the user making the request.
request.resource.data: the data being written.
firebase deploy --only firestore:rules
--only firestore:rules - Deploys only Firestore security rules without affecting other Firebase services.firebase emulators:start --only firestore
--only firestore - Starts only the Firestore emulator.curl -X GET 'http://localhost:8080/v1/projects/your-project-id/databases/(default)/documents/posts/post1'
curl -X PATCH 'http://localhost:8080/v1/projects/your-project-id/databases/(default)/documents/posts/post2' -H 'Content-Type: application/json' -d '{"fields": {"title": {"stringValue": "New Post"}, "content": {"stringValue": "Content here."}}}'
If you remember nothing else from this pattern, remember: resource and request objects let you control who can do what with your Firebase data and what data is allowed.
Practice
request object in Firebase functions mainly contain?Solution
Step 1: Understand the role of request objects
The request object holds details about the user's input and request method.Step 2: Differentiate from resource objects
Resource objects describe the trigger data or file, not user input.Final Answer:
Information about what the user sent and how -> Option DQuick Check:
Request object = user input info [OK]
- Confusing resource object with request object
- Thinking request holds trigger file info
- Assuming request contains project settings
Solution
Step 1: Recall request object structure
In Firebase HTTPS functions, user data is insiderequest.body.Step 2: Identify correct property for user data
The user data is accessed asrequest.body.dataif data is nested inside body.Final Answer:
request.body.data -> Option BQuick Check:
Access user data via request.body.data [OK]
- Using request.data.body which is invalid
- Trying to access request.payload which doesn't exist
- Confusing trigger data with request data
exports.myFunction = functions.storage.object().onFinalize((object, context) => {
console.log(object.name);
console.log(context.eventId);
});What does the
object parameter represent?Solution
Step 1: Identify the trigger type
The function triggers on a storage object finalization, meaning a file event.Step 2: Understand the object parameter
Theobjectparameter holds info about the file that triggered the function.Final Answer:
The file that triggered the function -> Option CQuick Check:
Storage trigger object = file info [OK]
- Thinking object is user request data
- Confusing context with resource object
- Assuming object holds config or logs
exports.func = functions.https.onRequest((resource, request) => {
console.log(request.body);
});Solution
Step 1: Check HTTPS function parameters
HTTPS functions receive (request, response) parameters in that order.Step 2: Identify parameter misuse
The code uses (resource, request) which reverses and misnames parameters.Final Answer:
Parameters are reversed; should be (request, response) -> Option AQuick Check:
HTTPS function params = (request, response) [OK]
- Swapping request and response parameters
- Using resource parameter in HTTPS function
- Ignoring parameter order importance
Solution
Step 1: Understand Firestore trigger parameters
Firestore triggers receive achangeobject withbeforeandaftersnapshots.Step 2: Access the new document data
The updated document data is accessed viachange.after.data(), which is the resource object.Final Answer:
resource.after.data() -> Option AQuick Check:
Firestore change after data = resource.after.data() [OK]
- Using request.body which is for HTTP functions
- Using context.params which holds path variables
- Using resource.data() which is old snapshot, not updated
Ready to prove it?
Pass the test and this pattern moves unknown
