Last active
August 11, 2021 16:08
-
-
Save phpmaps/9cdefd12f11086605cdeecfff1827055 to your computer and use it in GitHub Desktop.
authenticate-middlewear.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Reusable middlewear which checks to see if a user can modify comments | |
| const Campsite = require('./models/campsite'); | |
| exports.canModComments = async (req, res, next) => { | |
| const campsite = await Campsite.findById(req.params.campsiteId); | |
| if (campsite.comments.id(req.params.commentId).author.equals(req.user._id)) { | |
| req.campsite = campsite; //Add more stuff to the req object | |
| return next(); | |
| } else { | |
| const err = new Error('You are not authorized to perform this operation!'); | |
| err.status = 403; | |
| return next(err); | |
| } | |
| } |
Author
phpmaps
commented
Aug 11, 2021
Thank you for this! super helpful and looks much cleaner than all those ifs
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment