Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Resource Ownership Checks in Express
📖 Scenario: You are building a simple Express server for a blog. Each blog post belongs to a user. You want to make sure that only the owner of a post can edit or delete it.
🎯 Goal: Create an Express route that checks if the logged-in user owns the blog post before allowing updates or deletions.
📋 What You'll Learn
Create a sample posts data array with exact entries
Add a variable for the current logged-in user ID
Write a middleware function to check ownership of a post by ID
Use the middleware in a route to protect post editing
💡 Why This Matters
🌍 Real World
Web apps often need to make sure users can only change their own data, like posts or profiles.
💼 Career
Understanding resource ownership checks is key for backend developers to build secure APIs.
Progress0 / 4 steps
1
Create sample posts data
Create a variable called posts that is an array with these exact objects: { id: 1, title: 'First Post', ownerId: 101 }, { id: 2, title: 'Second Post', ownerId: 102 }, and { id: 3, title: 'Third Post', ownerId: 101 }.
Express
Hint
Use const posts = [ ... ] with the exact objects inside.
2
Set current logged-in user ID
Create a variable called currentUserId and set it to 101 to represent the logged-in user.
Express
Hint
Use const currentUserId = 101; exactly.
3
Create ownership check middleware
Write a middleware function called checkOwnership that takes req, res, next. It should get the post ID from req.params.id, find the post in posts, and if the post's ownerId is not equal to currentUserId, respond with status 403 and message 'Forbidden'. Otherwise, call next().
Express
Hint
Use req.params.id to get the post ID and check ownership. Call next() if allowed.
4
Use middleware in update route
Create an Express route handler for PUT /posts/:id that uses the checkOwnership middleware. The route should send a response with the text 'Post updated' if ownership check passes.
Express
Hint
Use app.put('/posts/:id', checkOwnership, (req, res) => { ... }) to protect the route.
Practice
(1/5)
1. What is the main purpose of resource ownership checks in an Express app?
easy
A. To allow any user to edit any resource
B. To ensure only the owner can access or modify their resource
C. To speed up database queries
D. To log user activity for analytics
Solution
Step 1: Understand resource ownership
Resource ownership means a resource belongs to a specific user.
Step 2: Purpose of ownership checks
Ownership checks prevent unauthorized users from accessing or changing resources they don't own.
Final Answer:
To ensure only the owner can access or modify their resource -> Option B
Quick Check:
Ownership check = restrict access to owner [OK]
Hint: Ownership checks block non-owners from resource access [OK]
Common Mistakes:
Thinking ownership checks speed up queries
Allowing all users to edit resources
Confusing ownership with logging
2. Which Express middleware pattern correctly checks if the logged-in user owns a resource with ID in req.params.id and owner ID in resource.ownerId?
easy
A. if (req.user.id == resource.owner) { next(); } else { res.status(401).send('Unauthorized'); }
B. if (req.user === resource.ownerId) { next(); } else { res.status(404).send('Not Found'); }
C. if (req.user.id === resource.ownerId) { next(); } else { res.status(403).send('Forbidden'); }
D. if (req.user.id !== resource.ownerId) { next(); } else { res.status(403).send('Forbidden'); }
Solution
Step 1: Check user ID equality
We compare req.user.id with resource.ownerId using strict equality to confirm ownership.
Step 2: Respond with 403 if not owner
If IDs don't match, respond with 403 Forbidden to block access.
Final Answer:
if (req.user.id === resource.ownerId) { next(); } else { res.status(403).send('Forbidden'); } -> Option C
B. The server will crash due to undefined resource
C. Response will be 404 Not Found
D. Response will be 403 Forbidden
Solution
Step 1: Compare user ID and owner ID
Since req.user.id ('123') does not equal resource.ownerId ('456'), ownership check fails.
Step 2: Return 403 Forbidden
The else block sends a 403 Forbidden response blocking deletion.
Final Answer:
Response will be 403 Forbidden -> Option D
Quick Check:
Non-matching IDs = 403 Forbidden [OK]
Hint: Non-owner gets 403 Forbidden response [OK]
Common Mistakes:
Assuming deletion happens anyway
Confusing 403 with 404
Ignoring ownership check logic
4. Identify the bug in this ownership check middleware:
function checkOwnership(req, res, next) {
const resource = {ownerId: '456'}; /* example */
if (req.user.id = resource.ownerId) {
next();
} else {
res.status(403).send('Forbidden');
}
}
medium
A. Using assignment (=) instead of comparison (===) in the if condition
B. Missing call to next() in else block
C. Incorrect status code; should be 404 instead of 403
D. resource.ownerId is undefined
Solution
Step 1: Check the if condition syntax
The condition uses single equals (=), which assigns instead of compares, causing a bug.
Step 2: Correct comparison operator
It should use strict equality (===) to compare req.user.id and resource.ownerId.
Final Answer:
Using assignment (=) instead of comparison (===) in the if condition -> Option A
Quick Check:
Assignment in if condition = bug [OK]
Hint: Use === for comparison, not = assignment [OK]
Common Mistakes:
Confusing = with === in conditions
Thinking next() needed in else block
Wrong status code for forbidden access
5. You want to protect a route so only the owner of a blog post can edit it. The post's owner ID is stored in post.ownerId. Which Express middleware correctly implements this ownership check and returns 403 if the user is not the owner?