Archived
1
0
Fork 0

Use middleware for user object on session and pass a test variable in case of... test

This commit is contained in:
Ethanell 2020-08-20 21:14:15 +02:00
parent 4201eae888
commit d98dcb0c07
17 changed files with 12 additions and 20 deletions

9
app.js
View file

@ -44,6 +44,12 @@ app.use(cookieParser());
app.use(session(sess)); app.use(session(sess));
app.use(express.static(path.join(__dirname, "public"))); app.use(express.static(path.join(__dirname, "public")));
app.use(i18n.init); 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("/", indexRouter);
app.use("/register", registerRouter); app.use("/register", registerRouter);
@ -58,7 +64,7 @@ app.use("/admin", adminRouter);
// catch 404 and forward to error handler // catch 404 and forward to error handler
app.use((req, res) => { app.use((req, res) => {
res.status(404); 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 // error handler
@ -66,7 +72,6 @@ app.use((err, req, res) => {
// render the error page // render the error page
res.status(err.status || 500); res.status(err.status || 500);
res.render("error", { res.render("error", {
user: req.session.user,
message: err.message, message: err.message,
error: req.app.get("env") === "development" ? err : {} error: req.app.get("env") === "development" ? err : {}
}); });

View file

@ -6,8 +6,7 @@ let error = require("../../utils/error");
router.get("/", sessionCheck(3), (req, res) => { router.get("/", sessionCheck(3), (req, res) => {
res.render("admin/departments/add", { res.render("admin/departments/add", {
title: "SOD - Departments administration", title: "SOD - Departments administration"
user: req.session.user
}); });
}).post("/", sessionCheck(3), async (req, res) => { }).post("/", sessionCheck(3), async (req, res) => {
if (!req.body.name) if (!req.body.name)

View file

@ -14,7 +14,6 @@ router.get("/", sessionCheck(3), async (req, res) => {
res.render("admin/departments/edit", { res.render("admin/departments/edit", {
title: "SOD - Departments administration", title: "SOD - Departments administration",
user: req.session.user,
department: department department: department
}); });
}).post("/", sessionCheck(3), async (req, res) => { }).post("/", sessionCheck(3), async (req, res) => {

View file

@ -8,7 +8,6 @@ let error = require("../../utils/error");
router.get("/", sessionCheck(3), async (req, res) => { router.get("/", sessionCheck(3), async (req, res) => {
res.render("admin/departments/index", { res.render("admin/departments/index", {
title: "SOD - Departments administration", title: "SOD - Departments administration",
user: req.session.user,
departments: await models.Department.findAll() departments: await models.Department.findAll()
}); });
}) })

View file

@ -7,7 +7,6 @@ let models = require("../../models");
router.get("/", sessionCheck(3), async (req, res) => { router.get("/", sessionCheck(3), async (req, res) => {
res.render("admin/index", { res.render("admin/index", {
title: "SOD - Administration", title: "SOD - Administration",
user: req.session.user,
sandwiches: await models.Sandwich.findAll(), sandwiches: await models.Sandwich.findAll(),
users: await models.User.findAll() users: await models.User.findAll()
}); });

View file

@ -8,7 +8,6 @@ let error = require("../utils/error");
router.get("/", sessionCheck(3), async (req, res) => { router.get("/", sessionCheck(3), async (req, res) => {
res.render("admin/orders", { res.render("admin/orders", {
title: "SOD - Orders administration", title: "SOD - Orders administration",
user: req.session.user,
orders: await models.Order.findAll({include: models.Sandwich, order: ["date"]}) orders: await models.Order.findAll({include: models.Sandwich, order: ["date"]})
}); });
}).post("/order/delete", sessionCheck(3), async (req, res) => { }).post("/order/delete", sessionCheck(3), async (req, res) => {

View file

@ -6,8 +6,7 @@ let error = require("../../utils/error");
router.get("/", sessionCheck(3), (req, res) => { router.get("/", sessionCheck(3), (req, res) => {
res.render("admin/sandwiches/add", { res.render("admin/sandwiches/add", {
title: "SOD - Sandwiches administration", title: "SOD - Sandwiches administration"
user: req.session.user
}); });
}).post("/", sessionCheck(3), async (req, res) => { }).post("/", sessionCheck(3), async (req, res) => {
if (!req.body.name || !req.body.price) if (!req.body.name || !req.body.price)

View file

@ -14,7 +14,6 @@ router.get("/", sessionCheck(3), async (req, res) => {
res.render("admin/sandwiches/edit", { res.render("admin/sandwiches/edit", {
title: "SOD - Sandwiches administration", title: "SOD - Sandwiches administration",
user: req.session.user,
sandwich: sandwich sandwich: sandwich
}); });
}).post("/", sessionCheck(3), async (req, res) => { }).post("/", sessionCheck(3), async (req, res) => {

View file

@ -8,7 +8,6 @@ let error = require("../../utils/error");
router.get("/", sessionCheck(3), async (req, res) => { router.get("/", sessionCheck(3), async (req, res) => {
res.render("admin/sandwiches/index", { res.render("admin/sandwiches/index", {
title: "SOD - Sandwiches administration", title: "SOD - Sandwiches administration",
user: req.session.user,
sandwiches: await models.Sandwich.findAll() sandwiches: await models.Sandwich.findAll()
}); });
}) })

View file

@ -7,7 +7,6 @@ let userCreate = require("../../utils/userCreate");
router.get("/", sessionCheck(3), async (req, res) => { router.get("/", sessionCheck(3), async (req, res) => {
res.render("admin/users/add", { res.render("admin/users/add", {
title: "SOD - Users administration", title: "SOD - Users administration",
user: req.session.user,
departments: await models.Department.findAll() departments: await models.Department.findAll()
}); });
}).post("/", sessionCheck(3), async (req, res) => { }).post("/", sessionCheck(3), async (req, res) => {

View file

@ -15,7 +15,6 @@ router.get("/", sessionCheck(3), async (req, res) => {
res.render("admin/users/edit", { res.render("admin/users/edit", {
title: "SOD - Users administration", title: "SOD - Users administration",
user: req.session.user,
targetUser: user, targetUser: user,
departments: await models.Department.findAll() departments: await models.Department.findAll()
}); });

View file

@ -8,7 +8,6 @@ let error = require("../../utils/error");
router.get("/", sessionCheck(3), async (req, res) => { router.get("/", sessionCheck(3), async (req, res) => {
res.render("admin/users/index", { res.render("admin/users/index", {
title: "SOD - Users administration", title: "SOD - Users administration",
user: req.session.user,
users: await models.User.findAll() users: await models.User.findAll()
}); });
}) })

View file

@ -5,7 +5,7 @@ let models = require("../models");
router.get("/", async (req, res) => { router.get("/", async (req, res) => {
let departments = await models.Department.findAll(); let departments = await models.Department.findAll();
let sandwiches = await models.Sandwich.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; module.exports = router;

View file

@ -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] = [];
orders[i.Order.DepartmentName][name][i.Order.id].push(i); 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; module.exports = router;

View file

@ -7,7 +7,6 @@ let userUpdate = require("./utils/userUpdate");
router.get("/", sessionCheck(0), async (req, res) => { router.get("/", sessionCheck(0), async (req, res) => {
res.render("profile", { res.render("profile", {
title: "SOD - Profile", title: "SOD - Profile",
user: req.session.user,
departments: await models.Department.findAll(), departments: await models.Department.findAll(),
orders: await models.Order.findAll({ orders: await models.Order.findAll({
where: {UserUsername: req.session.user.username}, where: {UserUsername: req.session.user.username},

View file

@ -10,7 +10,6 @@ router.get("/", sessionCheck(1), async (req, res) => {
res.render("sandwiches", { res.render("sandwiches", {
title: "SOD - Sandwiches", title: "SOD - Sandwiches",
user: req.session.user,
sandwiches: await models.SandwichOrder.findAll({ sandwiches: await models.SandwichOrder.findAll({
attributes: ["SandwichName", [sequelize.fn("COUNT", sequelize.col("SandwichName")), "number"]], attributes: ["SandwichName", [sequelize.fn("COUNT", sequelize.col("SandwichName")), "number"]],
where: {date: date}, where: {date: date},

View file

@ -1,4 +1,4 @@
module.exports = (req, res, message, status, subMessage) => { module.exports = (req, res, message, status, subMessage) => {
res.status(status || 500); 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}});
}; };