Consider this Express route that deletes a post only if the logged-in user owns it.
app.delete('/posts/:id', (req, res) => {
const post = posts.find(p => p.id === req.params.id);
if (!post) return res.status(404).send('Post not found');
if (post.ownerId !== req.user.id) return res.status(403).send('Forbidden');
posts = posts.filter(p => p.id !== req.params.id);
res.send('Deleted');
});If req.user.id is 'user2' and the post owner is 'user1', what will the response be?
app.delete('/posts/:id', (req, res) => { const post = posts.find(p => p.id === req.params.id); if (!post) return res.status(404).send('Post not found'); if (post.ownerId !== req.user.id) return res.status(403).send('Forbidden'); posts = posts.filter(p => p.id !== req.params.id); res.send('Deleted'); });
Check the ownership condition before deleting.
The code checks if the post owner matches the logged-in user. If not, it returns 403 Forbidden.
Choose the correct middleware snippet that verifies if the logged-in user owns the resource before proceeding.
Remember to use strict comparison and call next() only if ownership matches.
Option A correctly compares owner id and user id, returning 403 if they differ, else calling next().
Examine this Express route:
app.delete('/items/:id', (req, res) => {
const item = items.find(i => i.id === req.params.id);
if (!item) return res.status(404).send('Not found');
if (item.ownerId = req.user.id) {
items = items.filter(i => i.id !== req.params.id);
return res.send('Deleted');
}
res.status(403).send('Forbidden');
});Why does this code allow any user to delete any item?
app.delete('/items/:id', (req, res) => { const item = items.find(i => i.id === req.params.id); if (!item) return res.status(404).send('Not found'); if (item.ownerId = req.user.id) { items = items.filter(i => i.id !== req.params.id); return res.send('Deleted'); } res.status(403).send('Forbidden'); });
Look carefully at the if condition syntax.
The single '=' assigns req.user.id to item.ownerId, which always evaluates to truthy, allowing deletion.
Given this initial posts array:
let posts = [
{ id: '1', ownerId: 'user1' },
{ id: '2', ownerId: 'user2' },
{ id: '3', ownerId: 'user1' }
];And this Express route:
app.delete('/posts/:id', (req, res) => {
const post = posts.find(p => p.id === req.params.id);
if (!post) return res.status(404).send('Not found');
if (post.ownerId !== req.user.id) return res.status(403).send('Forbidden');
posts = posts.filter(p => p.id !== req.params.id);
res.send('Deleted');
});If req.user.id is 'user1' and the request is to delete post with id '2', what will be the posts array after the request?
let posts = [
{ id: '1', ownerId: 'user1' },
{ id: '2', ownerId: 'user2' },
{ id: '3', ownerId: 'user1' }
];
app.delete('/posts/:id', (req, res) => {
const post = posts.find(p => p.id === req.params.id);
if (!post) return res.status(404).send('Not found');
if (post.ownerId !== req.user.id) return res.status(403).send('Forbidden');
posts = posts.filter(p => p.id !== req.params.id);
res.send('Deleted');
});Check if the ownership condition allows deletion.
The user 'user1' tries to delete post '2' owned by 'user2', so deletion is forbidden and posts remain unchanged.
Choose the best approach to enforce resource ownership checks across multiple routes in an Express application.
Think about code reuse and security.
Using middleware centralizes ownership checks, avoids repetition, and improves security by verifying ownership before any modification.