-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
63 lines (59 loc) · 1.96 KB
/
index.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
var middlewareObj = {};
var Campground = require("../models/campground")
var Comment = require("../models/comments")
middlewareObj.checkCampgroundOwnership = function(req,res,next)
{
if(req.isAuthenticated())
{
Campground.findById(req.params.id,function(err,foundCampground){
if(err || !foundCampground){
req.flash("error","Campground Not Found");
console.log(err);
res.redirect("back")
}
else{
if(foundCampground.author.id.equals(req.user._id) || req.user.isAdmin)
{
next();
}
else
{
req.flash("error","You don't have permission to do that")
res.redirect("back");
}
}
})
}
};
middlewareObj.checkCommentOwnership = function(req,res,next)
{
if(req.isAuthenticated())
{
Comment.findById(req.params.id2,function(err,foundComment){
if(err || !foundComment){
console.log(err);
req.flash("error","Comment not found")
res.redirect("back")
}
else{
if(foundComment.author.id.equals(req.user._id)||req.user.isAdmin)
{
next();
}
else{
res.redirect("back");
}
}
})
}
};
middlewareObj.isLoggedIn = function(req,res,next){
if(req.isAuthenticated()){
return next();
}
else{
req.flash("error","You need to be logged in to do that")
res.redirect("/login");
}
}
module.exports = middlewareObj;