Skip to content

Commit

Permalink
refactor: Move all the middlewares into separate dir
Browse files Browse the repository at this point in the history
  • Loading branch information
pnijhara committed Jun 14, 2020
1 parent ccf734d commit 14285dd
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 33 deletions.
33 changes: 33 additions & 0 deletions middleware/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
var foodData = require("../models/foodup")

// all the middleare goes here
var middlewareObj = {};

middlewareObj.isFoodPostOnwer = function(req, res, next) {
if(req.isAuthenticated()){
foodData.findById(req.params.id, function(err, foundFoodData){
if(err){
res.redirect("back")
} else{
if(foundFoodData.author.id.equals(req.user.id)){
next()
} else{
res.redirect("back")
}
}
})
}
else {
res.redirect("back")
}
}


middlewareObj.isLoggedIn = function(req, res, next){
if(req.isAuthenticated()){
return next()
}
res.redirect("/login")
}

module.exports = middlewareObj;
39 changes: 6 additions & 33 deletions routes/FoodUp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const express = require("express")
const router = express.Router()
const foodData = require("../models/foodup")
const middleware = require("../middleware")

//FoodUp route//
//SHOW route//
Expand All @@ -15,11 +16,11 @@ router.get("/FoodUp", function(_req, res){
})

//CREATE food route//
router.get("/FoodUp/new", isLoggedIn, function(_req, res){
router.get("/FoodUp/new", middleware.isLoggedIn, function(_req, res){
res.render("food-up/new")
})

router.post("/FoodUp", isLoggedIn, function(req, res){
router.post("/FoodUp", middleware.isLoggedIn, function(req, res){
const name = req.body.name
const description = req.body.description
const image = req.body.image
Expand Down Expand Up @@ -53,7 +54,7 @@ router.get("/FoodUp/:id", function(req, res){
})

//EDIT food route//
router.get("/FoodUp/:id/edit",isFoodPostOnwer, function(req, res){
router.get("/FoodUp/:id/edit", middleware.isFoodPostOnwer, function(req, res){
foodData.findById(req.params.id, function(err, foundFoodData){
if(err){
res.redirect("/FoodUp/" + foodData.id)
Expand All @@ -64,7 +65,7 @@ router.get("/FoodUp/:id/edit",isFoodPostOnwer, function(req, res){
})

//UPDATE ROUTE//
router.put("/FoodUp/:id",isFoodPostOnwer, function(req, res){
router.put("/FoodUp/:id", middleware.isFoodPostOnwer, function(req, res){
const updatedFoodData = {
name: req.body.name,
description: req.body.description,
Expand All @@ -80,7 +81,7 @@ router.put("/FoodUp/:id",isFoodPostOnwer, function(req, res){
})

//DELETE ROUTE
router.delete("/FoodUp/:id",isFoodPostOnwer, function(req, res){
router.delete("/FoodUp/:id", middleware.isFoodPostOnwer, function(req, res){
foodData.findByIdAndRemove(req.params.id, function(err){
if(err){
console.log(err)
Expand All @@ -90,32 +91,4 @@ router.delete("/FoodUp/:id",isFoodPostOnwer, function(req, res){
})
})

//isLoggedIn middleware
function isLoggedIn(req, res, next){
if(req.isAuthenticated()){
return next()
}
res.redirect("/login")
}

//isFoodPostOwner middleware
function isFoodPostOnwer(req, res, next){
if(req.isAuthenticated()){
foodData.findById(req.params.id, function(err, foundFoodData){
if(err){
res.redirect("back")
} else{
if(foundFoodData.author.id.equals(req.user.id)){
next()
} else{
res.redirect("back")
}
}
})
}
else {
res.redirect("back")
}
}

module.exports = router

0 comments on commit 14285dd

Please sign in to comment.