-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.js
69 lines (48 loc) · 1.88 KB
/
middleware.js
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
var Post = require("./models/trips");
var Comment = require("./models/comment");
var middlewareObj={};
middlewareObj.checkOwnership = function ( req, res, next )
{
if(req.isAuthenticated())
{
Post.findById(req.params.id, function(err, foundCampground)
{ if(err) {
req.flash("error", "Campground not found");
res.redirect("back");}
else{
// does this user owns camps
if(foundCampground.author.id.equals(req.user._id))
{ next();}
else { req.flash("error", "You don't have permission to do that");
res.redirect("back");}
}
});
} else { req.flash("error", "You need to be logged in to do that");
res.redirect("back");}
};
middlewareObj.isLoggedIn = function(req, res, next){
if(req.isAuthenticated()){
return next();
}
req.flash("error", "You need to be logged in to do that");
res.redirect("/login");
}
middlewareObj.checkCommentOwnership = function(req, res, next) {
if(req.isAuthenticated()){
Comment.findById(req.params.comment_id, function(err, foundComment){
if(err){
res.redirect("back");
} else {
// does user own the comment?
if(foundComment.author.id.equals(req.user._id)) {
next();
} else {
res.redirect("back");
}
}
});
} else {
res.redirect("back");
}
}
module.exports = middlewareObj;