0
0
Expressframework~10 mins

Resource ownership checks in Express - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Resource ownership checks
Request received
Extract user ID from auth
Extract resource ID from request
Fetch resource from database
Compare resource owner ID with user ID
Allow access
Send response
The server checks if the logged-in user owns the resource before allowing access or denying it.
Execution Sample
Express
app.get('/posts/:id', async (req, res) => {
  const userId = req.user.id;
  const post = await Post.findById(req.params.id);
  if (post && post.ownerId === userId) {
    res.send(post);
  } else {
    res.status(403).send('Forbidden');
  }
});
This code checks if the logged-in user owns the post before sending it or denying access.
Execution Table
StepActionValue/User IDResource Owner IDCondition (ownerId === userId)Branch TakenResponse Sent
1Request received for post ID 101user123----
2Fetch post from DBuser123user123---
3Compare ownerId and userIduser123user123trueAllow access-
4Send post datauser123user123trueAllow accessPost data sent
5Request received for post ID 102user123----
6Fetch post from DBuser123user999---
7Compare ownerId and userIduser123user999falseDeny access-
8Send 403 Forbiddenuser123user999falseDeny access403 Forbidden sent
💡 Execution stops after sending response based on ownership check result.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 6After Step 7Final
userIdundefineduser123user123user123user123user123
post.ownerIdundefineduser123user123user999user999user999
condition ownerId === userIdundefined-true-falsefalse
responsenonenonependingnonependingsent
Key Moments - 3 Insights
Why do we compare post.ownerId with userId?
We compare to check if the logged-in user owns the resource. If they match (see execution_table step 3), access is allowed; otherwise, it is denied (step 7).
What happens if the resource is not found in the database?
If the resource is missing, the fetch returns null or undefined, so ownerId is undefined. The condition fails, and access is denied, preventing unauthorized access.
Why send a 403 status code instead of 404 when ownership check fails?
403 means 'Forbidden'—the resource exists but user can't access it. 404 means 'Not Found'. Using 403 informs the user they are unauthorized, not that the resource is missing.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the condition result at step 7?
Afalse
Bundefined
Ctrue
Dnull
💡 Hint
Check the 'Condition (ownerId === userId)' column at step 7 in the execution_table.
At which step is the 403 Forbidden response sent?
AStep 4
BStep 8
CStep 5
DStep 3
💡 Hint
Look at the 'Response Sent' column in the execution_table for the step sending '403 Forbidden sent'.
If userId and ownerId always match, what changes in the execution table?
ANo response is sent
BAll branches take 'Deny access' path
CAll branches take 'Allow access' path
DThe condition is always false
💡 Hint
Refer to the 'Branch Taken' column and condition results in the execution_table.
Concept Snapshot
Resource ownership checks in Express:
- Extract user ID from authenticated request
- Fetch resource by ID from DB
- Compare resource owner ID with user ID
- If match, allow access and send resource
- If no match, send 403 Forbidden
- Prevents unauthorized access to others' data
Full Transcript
In Express, resource ownership checks ensure users can only access their own data. When a request arrives, the server extracts the logged-in user's ID and the resource ID from the request. It fetches the resource from the database and compares the resource's owner ID with the user's ID. If they match, the server sends the resource data back. If they don't, the server responds with a 403 Forbidden status, denying access. This process protects user data by verifying ownership before allowing access.