From d98dcb0c07e59f30437b435e0c40364769caccd2 Mon Sep 17 00:00:00 2001 From: flifloo Date: Thu, 20 Aug 2020 21:14:15 +0200 Subject: [PATCH] Use middleware for user object on session and pass a test variable in case of... test --- app.js | 9 +++++++-- routes/admin/departments/add.js | 3 +-- routes/admin/departments/edit.js | 1 - routes/admin/departments/index.js | 1 - routes/admin/index.js | 1 - routes/admin/orders.js | 1 - routes/admin/sandwiches/add.js | 3 +-- routes/admin/sandwiches/edit.js | 1 - routes/admin/sandwiches/index.js | 1 - routes/admin/users/add.js | 1 - routes/admin/users/edit.js | 1 - routes/admin/users/index.js | 1 - routes/index.js | 2 +- routes/orders.js | 2 +- routes/profile.js | 1 - routes/sandwiches.js | 1 - routes/utils/error.js | 2 +- 17 files changed, 12 insertions(+), 20 deletions(-) diff --git a/app.js b/app.js index 6d99b94..ba9893d 100644 --- a/app.js +++ b/app.js @@ -44,6 +44,12 @@ app.use(cookieParser()); app.use(session(sess)); app.use(express.static(path.join(__dirname, "public"))); app.use(i18n.init); +if(process.env.NODE_ENV === "test") + app.locals.test = true; +app.use((req, res, next) => { + res.locals.user = req.session.user; + next(); +}); app.use("/", indexRouter); app.use("/register", registerRouter); @@ -58,7 +64,7 @@ app.use("/admin", adminRouter); // catch 404 and forward to error handler app.use((req, res) => { res.status(404); - res.render("error", {title: "SOD - Page not found", user: req.session.user, message: "Page not found", "error": {}}) + res.render("error", {title: "SOD - Page not found", message: "Page not found", "error": {}}) }); // error handler @@ -66,7 +72,6 @@ app.use((err, req, res) => { // render the error page res.status(err.status || 500); res.render("error", { - user: req.session.user, message: err.message, error: req.app.get("env") === "development" ? err : {} }); diff --git a/routes/admin/departments/add.js b/routes/admin/departments/add.js index 251dd20..246d686 100644 --- a/routes/admin/departments/add.js +++ b/routes/admin/departments/add.js @@ -6,8 +6,7 @@ let error = require("../../utils/error"); router.get("/", sessionCheck(3), (req, res) => { res.render("admin/departments/add", { - title: "SOD - Departments administration", - user: req.session.user + title: "SOD - Departments administration" }); }).post("/", sessionCheck(3), async (req, res) => { if (!req.body.name) diff --git a/routes/admin/departments/edit.js b/routes/admin/departments/edit.js index 89a5052..4ccf0f8 100644 --- a/routes/admin/departments/edit.js +++ b/routes/admin/departments/edit.js @@ -14,7 +14,6 @@ router.get("/", sessionCheck(3), async (req, res) => { res.render("admin/departments/edit", { title: "SOD - Departments administration", - user: req.session.user, department: department }); }).post("/", sessionCheck(3), async (req, res) => { diff --git a/routes/admin/departments/index.js b/routes/admin/departments/index.js index ef4eaa1..f6116fb 100644 --- a/routes/admin/departments/index.js +++ b/routes/admin/departments/index.js @@ -8,7 +8,6 @@ let error = require("../../utils/error"); router.get("/", sessionCheck(3), async (req, res) => { res.render("admin/departments/index", { title: "SOD - Departments administration", - user: req.session.user, departments: await models.Department.findAll() }); }) diff --git a/routes/admin/index.js b/routes/admin/index.js index d031186..12aed57 100644 --- a/routes/admin/index.js +++ b/routes/admin/index.js @@ -7,7 +7,6 @@ let models = require("../../models"); router.get("/", sessionCheck(3), async (req, res) => { res.render("admin/index", { title: "SOD - Administration", - user: req.session.user, sandwiches: await models.Sandwich.findAll(), users: await models.User.findAll() }); diff --git a/routes/admin/orders.js b/routes/admin/orders.js index 81c1ea9..0b9bb8e 100644 --- a/routes/admin/orders.js +++ b/routes/admin/orders.js @@ -8,7 +8,6 @@ let error = require("../utils/error"); router.get("/", sessionCheck(3), async (req, res) => { res.render("admin/orders", { title: "SOD - Orders administration", - user: req.session.user, orders: await models.Order.findAll({include: models.Sandwich, order: ["date"]}) }); }).post("/order/delete", sessionCheck(3), async (req, res) => { diff --git a/routes/admin/sandwiches/add.js b/routes/admin/sandwiches/add.js index 3ad2800..2cd5e10 100644 --- a/routes/admin/sandwiches/add.js +++ b/routes/admin/sandwiches/add.js @@ -6,8 +6,7 @@ let error = require("../../utils/error"); router.get("/", sessionCheck(3), (req, res) => { res.render("admin/sandwiches/add", { - title: "SOD - Sandwiches administration", - user: req.session.user + title: "SOD - Sandwiches administration" }); }).post("/", sessionCheck(3), async (req, res) => { if (!req.body.name || !req.body.price) diff --git a/routes/admin/sandwiches/edit.js b/routes/admin/sandwiches/edit.js index 0ae55fb..a54666d 100644 --- a/routes/admin/sandwiches/edit.js +++ b/routes/admin/sandwiches/edit.js @@ -14,7 +14,6 @@ router.get("/", sessionCheck(3), async (req, res) => { res.render("admin/sandwiches/edit", { title: "SOD - Sandwiches administration", - user: req.session.user, sandwich: sandwich }); }).post("/", sessionCheck(3), async (req, res) => { diff --git a/routes/admin/sandwiches/index.js b/routes/admin/sandwiches/index.js index 2f1f323..61b1f0c 100644 --- a/routes/admin/sandwiches/index.js +++ b/routes/admin/sandwiches/index.js @@ -8,7 +8,6 @@ let error = require("../../utils/error"); router.get("/", sessionCheck(3), async (req, res) => { res.render("admin/sandwiches/index", { title: "SOD - Sandwiches administration", - user: req.session.user, sandwiches: await models.Sandwich.findAll() }); }) diff --git a/routes/admin/users/add.js b/routes/admin/users/add.js index e2b8fc8..1f31e37 100644 --- a/routes/admin/users/add.js +++ b/routes/admin/users/add.js @@ -7,7 +7,6 @@ let userCreate = require("../../utils/userCreate"); router.get("/", sessionCheck(3), async (req, res) => { res.render("admin/users/add", { title: "SOD - Users administration", - user: req.session.user, departments: await models.Department.findAll() }); }).post("/", sessionCheck(3), async (req, res) => { diff --git a/routes/admin/users/edit.js b/routes/admin/users/edit.js index db038fc..b8bc7c1 100644 --- a/routes/admin/users/edit.js +++ b/routes/admin/users/edit.js @@ -15,7 +15,6 @@ router.get("/", sessionCheck(3), async (req, res) => { res.render("admin/users/edit", { title: "SOD - Users administration", - user: req.session.user, targetUser: user, departments: await models.Department.findAll() }); diff --git a/routes/admin/users/index.js b/routes/admin/users/index.js index fa99688..1f592b4 100644 --- a/routes/admin/users/index.js +++ b/routes/admin/users/index.js @@ -8,7 +8,6 @@ let error = require("../../utils/error"); router.get("/", sessionCheck(3), async (req, res) => { res.render("admin/users/index", { title: "SOD - Users administration", - user: req.session.user, users: await models.User.findAll() }); }) diff --git a/routes/index.js b/routes/index.js index f6543a2..8d3f130 100644 --- a/routes/index.js +++ b/routes/index.js @@ -5,7 +5,7 @@ let models = require("../models"); router.get("/", async (req, res) => { let departments = await models.Department.findAll(); let sandwiches = await models.Sandwich.findAll(); - res.render("index", { title: "SOD - Home", user: req.session.user, departments: departments, sandwiches: sandwiches }); + res.render("index", { title: "SOD - Home", departments: departments, sandwiches: sandwiches }); }); module.exports = router; diff --git a/routes/orders.js b/routes/orders.js index eab19d0..94369a4 100644 --- a/routes/orders.js +++ b/routes/orders.js @@ -23,7 +23,7 @@ router.get("/", sessionCheck(2), async (req, res) => { orders[i.Order.DepartmentName][name][i.Order.id] = []; orders[i.Order.DepartmentName][name][i.Order.id].push(i); } - res.render("orders", {title: "SOD - Orders", user: req.session.user, orders: orders, date: date}); + res.render("orders", {title: "SOD - Orders", orders: orders, date: date}); }); module.exports = router; diff --git a/routes/profile.js b/routes/profile.js index ad1f167..862d4a9 100644 --- a/routes/profile.js +++ b/routes/profile.js @@ -7,7 +7,6 @@ let userUpdate = require("./utils/userUpdate"); router.get("/", sessionCheck(0), async (req, res) => { res.render("profile", { title: "SOD - Profile", - user: req.session.user, departments: await models.Department.findAll(), orders: await models.Order.findAll({ where: {UserUsername: req.session.user.username}, diff --git a/routes/sandwiches.js b/routes/sandwiches.js index 571f41f..c097c73 100644 --- a/routes/sandwiches.js +++ b/routes/sandwiches.js @@ -10,7 +10,6 @@ router.get("/", sessionCheck(1), async (req, res) => { res.render("sandwiches", { title: "SOD - Sandwiches", - user: req.session.user, sandwiches: await models.SandwichOrder.findAll({ attributes: ["SandwichName", [sequelize.fn("COUNT", sequelize.col("SandwichName")), "number"]], where: {date: date}, diff --git a/routes/utils/error.js b/routes/utils/error.js index ab48b38..81799dc 100644 --- a/routes/utils/error.js +++ b/routes/utils/error.js @@ -1,4 +1,4 @@ module.exports = (req, res, message, status, subMessage) => { res.status(status || 500); - res.render("error", {user: req.session.user, message: message, error: {status: subMessage || undefined}}); + res.render("error", {message: message, error: {status: subMessage || undefined}}); };