Skip to content

Commit bedb109

Browse files
committed
Refactor middleware in YelpCamp project
1 parent 54f9a4d commit bedb109

File tree

3 files changed

+65
-66
lines changed

3 files changed

+65
-66
lines changed

YelpCamp/middleware/index.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
var Campground = require("../models/campground");
2+
var Comment = require("../models/comment");
3+
4+
var middlewareObj = {};
5+
6+
middlewareObj.checkCampgroundOwnership = function(req, res, next) {
7+
if(req.isAuthenticated()) {
8+
Campground.findById(req.params.id, function(err, foundCampground) {
9+
if(err) {
10+
res.redirect("/campgrounds");
11+
} else {
12+
// does user own the camground?
13+
if(foundCampground.author.id.equals(req.user._id)) {
14+
next();
15+
} else {
16+
res.redirect("back");
17+
}
18+
}
19+
});
20+
} else {
21+
res.redirect("back");
22+
}
23+
}
24+
25+
middlewareObj.checkCommentOwnership = function(req, res, next) {
26+
if(req.isAuthenticated()) {
27+
Comment.findById(req.params.comment_id, function(err, foundComment) {
28+
if(err) {
29+
res.redirect("back");
30+
} else {
31+
// does user own the comment?
32+
if(foundComment.author.id.equals(req.user._id)) {
33+
next();
34+
} else {
35+
res.redirect("back");
36+
}
37+
}
38+
});
39+
} else {
40+
res.redirect("back");
41+
}
42+
}
43+
44+
middlewareObj.isLoggedIn = function(req, res, next){
45+
if(req.isAuthenticated()){
46+
return next();
47+
}
48+
res.redirect("/login");
49+
}
50+
51+
52+
53+
module.exports = middlewareObj;

YelpCamp/routes/campgrounds.js

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var express = require('express');
22
var router = express.Router();
33
var Campground = require("../models/campground");
4+
var middleware = require("../middleware");
45

56
// INDEX - show all campgrounds
67
router.get("/", function(req, res) {
@@ -16,7 +17,7 @@ router.get("/", function(req, res) {
1617
});
1718

1819
// NEW - show form to create new campground
19-
router.get("/new", isLoggedIn, function(req, res) {
20+
router.get("/new", middleware.isLoggedIn, function(req, res) {
2021
res.render("campgrounds/new")
2122
});
2223

@@ -35,7 +36,7 @@ router.get("/:id", function(req, res) {
3536
});
3637

3738
// CREATE - add new campground to DB
38-
router.post("/", isLoggedIn, function(req, res) {
39+
router.post("/", middleware.isLoggedIn, function(req, res) {
3940
// get data from form and add to campgrounds array
4041
var name = req.body.name;
4142
var image = req.body.image;
@@ -62,14 +63,14 @@ router.post("/", isLoggedIn, function(req, res) {
6263
});
6364

6465
// EDIT Campground route
65-
router.get("/:id/edit", checkCampgroundOwnership, function(req, res) {
66+
router.get("/:id/edit", middleware.checkCampgroundOwnership, function(req, res) {
6667
Campground.findById(req.params.id, function(err, foundCampground) {
6768
res.render("campgrounds/edit", {campground: foundCampground});
6869
});
6970
});
7071

7172
// UPDATE Campground route
72-
router.put("/:id", checkCampgroundOwnership, function(req, res) {
73+
router.put("/:id", middleware.checkCampgroundOwnership, function(req, res) {
7374
// find and update the correct campground
7475
Campground.findByIdAndUpdate(req.params.id, req.body.campground, function(err, updatedCamground) {
7576
if(err) {
@@ -82,7 +83,7 @@ router.put("/:id", checkCampgroundOwnership, function(req, res) {
8283
});
8384

8485
// DESTROY Campground Route
85-
router.delete("/:id/", checkCampgroundOwnership, function(req, res) {
86+
router.delete("/:id/", middleware.checkCampgroundOwnership, function(req, res) {
8687
Campground.findByIdAndRemove(req.params.id, function(err) {
8788
if(err) {
8889
res.redirect("/campgrounds");
@@ -92,32 +93,4 @@ router.delete("/:id/", checkCampgroundOwnership, function(req, res) {
9293
});
9394
});
9495

95-
// middleware
96-
function checkCampgroundOwnership(req, res, next) {
97-
if(req.isAuthenticated()) {
98-
Campground.findById(req.params.id, function(err, foundCampground) {
99-
if(err) {
100-
res.redirect("/campgrounds");
101-
} else {
102-
// does user own the camground?
103-
if(foundCampground.author.id.equals(req.user._id)) {
104-
next();
105-
} else {
106-
res.redirect("back");
107-
}
108-
}
109-
});
110-
} else {
111-
res.redirect("back");
112-
}
113-
}
114-
115-
// middleware
116-
function isLoggedIn(req, res, next){
117-
if(req.isAuthenticated()){
118-
return next();
119-
}
120-
res.redirect("/login");
121-
}
122-
12396
module.exports = router;

YelpCamp/routes/comments.js

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ var express = require('express');
22
var router = express.Router({mergeParams: true}); // mergeParams will pass req.params in this file
33
var Campground = require("../models/campground");
44
var Comment = require("../models/comment");
5+
var middleware = require("../middleware");
56

67
// Comments New
7-
router.get("/new", isLoggedIn, function(req, res) {
8+
router.get("/new", middleware.isLoggedIn, function(req, res) {
89
// Find campground by ID
910
Campground.findById(req.params.id, function(err, campground) {
1011
if(err) {
@@ -16,7 +17,7 @@ router.get("/new", isLoggedIn, function(req, res) {
1617
});
1718

1819
// Comments Create
19-
router.post("/", isLoggedIn, function(req, res) {
20+
router.post("/", middleware.isLoggedIn, function(req, res) {
2021
// lookup campgroud using ID
2122
Campground.findById(req.params.id, function(err, campground) {
2223
if(err) {
@@ -45,7 +46,7 @@ router.post("/", isLoggedIn, function(req, res) {
4546
});
4647

4748
// Show edit form for Comment
48-
router.get("/:comment_id/edit", checkCommentOwnership, function(req, res) {
49+
router.get("/:comment_id/edit", middleware.checkCommentOwnership, function(req, res) {
4950
Comment.findById(req.params.comment_id, function(err, foundComment) {
5051
if(err) {
5152
res.redirect("back");
@@ -56,7 +57,7 @@ router.get("/:comment_id/edit", checkCommentOwnership, function(req, res) {
5657
});
5758

5859
// UPDATE comment
59-
router.put("/:comment_id", checkCommentOwnership, function(req, res) {
60+
router.put("/:comment_id", middleware.checkCommentOwnership, function(req, res) {
6061
Comment.findByIdAndUpdate(req.params.comment_id, req.body.comment, function(err, updatedComment){
6162
if(err) {
6263
res.redirect("back");
@@ -67,7 +68,7 @@ router.put("/:comment_id", checkCommentOwnership, function(req, res) {
6768
});
6869

6970
// DESTROY comment route
70-
router.delete("/:comment_id", checkCommentOwnership, function(req, res) {
71+
router.delete("/:comment_id", middleware.checkCommentOwnership, function(req, res) {
7172
Comment.findByIdAndRemove(req.params.comment_id, function(err) {
7273
if(err) {
7374
res.redirect("back");
@@ -77,32 +78,4 @@ router.delete("/:comment_id", checkCommentOwnership, function(req, res) {
7778
});
7879
});
7980

80-
// middleware
81-
function checkCommentOwnership(req, res, next) {
82-
if(req.isAuthenticated()) {
83-
Comment.findById(req.params.comment_id, function(err, foundComment) {
84-
if(err) {
85-
res.redirect("back");
86-
} else {
87-
// does user own the comment?
88-
if(foundComment.author.id.equals(req.user._id)) {
89-
next();
90-
} else {
91-
res.redirect("back");
92-
}
93-
}
94-
});
95-
} else {
96-
res.redirect("back");
97-
}
98-
}
99-
100-
// middleware
101-
function isLoggedIn(req, res, next){
102-
if(req.isAuthenticated()){
103-
return next();
104-
}
105-
res.redirect("/login");
106-
}
107-
10881
module.exports = router;

0 commit comments

Comments
 (0)