Deleting a post

The delete button is only visible if the signed-in user and postedBy user are the same for the specific post being rendered. For the post to be deleted from the database, we will have to set up a delete post API that will also have a fetch method in the frontend to be applied when delete is clicked.

mern-social/server/routes/post.routes.js:

router.route('/api/posts/:postId')    .delete(authCtrl.requireSignin,               postCtrl.isPoster,                   postCtrl.remove)

The delete route will check for authorization before calling remove on the post, by ensuring the authenticated user and postedBy user are the same usersThe isPoster method checks if the signed-in user is the original creator of the post before executing the next method.

mern-social/server ...

Get Full-Stack React Projects now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.