-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.js
More file actions
27 lines (23 loc) · 759 Bytes
/
middleware.js
File metadata and controls
27 lines (23 loc) · 759 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
module.exports.isLoggedIn = (req, res, next) => {
if (!req.isAuthenticated()) {
req.flash('error', 'You need to login first');
return res.redirect('/login');
}
next();
}
module.exports.isRetailer = (req, res, next) => {
if (req.user.userType === 'retailer') {
return next();
}
req.flash('error', 'You are not authorised to do that');
res.redirect('/products');
}
module.exports.isProductAuthor = async(req, res, next) => {
const { productid } = req.params;
const product = await Product.findById(productid);
if (product.author && product.author.equals(req.user._id)) {
return next();
}
req.flash('error', 'You are not authorised to do that');
res.redirect('/products');
}