Add Username to Server
We are going to jump back into the server and check for the username in the http request and match it to the username of the idea. If it matches, we will update/delete. If not, we will send a 403 error.
Go to the backend routes/idea.js file and add update the delete request function
// Delete idea
router.delete('/:id', async (req, res) => {
try {
const idea = await Idea.findById(req.params.id);
// Match the usernames
if (idea.username === req.body.username) {
await Idea.findByIdAndDelete(req.params.id);
return res.json({ success: true, data: {} });
}
// Usernames do not match
res.status(403).json({
success: false,
error: 'You are not authorized to delete this resource',
});
} catch (error) {
console.log(error);
res.status(500).json({ success: false, error: 'Something went wrong' });
}
});
We want to do the same thing with the update:
// Update idea
router.put('/:id', async (req, res) => {
try {
const idea = await Idea.findById(req.params.id);
// Match the usernames
if (idea.username === req.body.username) {
const updatedIdea = await Idea.findByIdAndUpdate(
req.params.id,
{
$set: {
text: req.body.text,
tag: req.body.tag,
},
},
{ new: true }
);
return res.json({ success: true, data: updatedIdea });
}
// Usernames do not match
res.status(403).json({
success: false,
error: 'You are not authorized to update this resource',
});
} catch (error) {
console.log(error);
res.status(500).json({ success: false, error: 'Something went wrong' });
}
});
Now, you can try to make an update or delete from Postman and see if it works. you need to send the username in the request body and it has to match the idea username.