-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddlewares.js
More file actions
38 lines (34 loc) · 1.12 KB
/
Copy pathmiddlewares.js
File metadata and controls
38 lines (34 loc) · 1.12 KB
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
28
29
30
31
32
33
34
35
36
37
38
const Listing = require("./models/listings");
const Review = require("./models/reviews");
module.exports.isLoggedIn = (req,res,next)=>{
if (!req.isAuthenticated()) {
req.session.redirectUrl = req.originalUrl;
req.flash("error", "You must be Logged in to Create a Listing");
return res.redirect("/login");
}
next();
};
module.exports.savedurl = (req,res,next)=>{
if(req.session.redirectUrl){
res.locals.redirectUrl = req.session.redirectUrl;
}
next();
};
module.exports.isOwner = async (req,res,next)=>{
let { id } = req.params;
let list = await Listing.findById(id);
if(!list.owner._id.equals(res.locals.curruser._id)){
req.flash("error","you don't have access.");
return res.redirect(`/listings`);
}
next();
}
module.exports.isReviewOwner = async (req,res,next)=>{
let { reviewID } = req.params;
let review = await Review.findById(reviewID);
if(!review.author._id.equals(res.locals.curruser._id)){
req.flash("error","you don't have access.");
return res.redirect(`/listings`);
}
next();
}