1
0
Fork 0

Setup permissions check on index

This commit is contained in:
Ethanell 2020-10-12 23:34:51 +02:00
parent b73136b6ac
commit 0ae014549d
4 changed files with 24 additions and 5 deletions

6
app.js
View file

@ -1,10 +1,10 @@
let createError = require("http-errors");
let express = require("express");
let path = require("path");
let cookieParser = require("cookie-parser");
let logger = require("morgan");
let sassMiddleware = require("node-sass-middleware");
const session = require("express-session");
const error = require("./routes/utils/error");
let config = process.env.NODE_ENV === "test" ? {} : require("./config/config.json");
let indexRouter = require("./routes/index");
@ -42,8 +42,8 @@ app.use("/", indexRouter);
app.use("/login", loginRouter);
// catch 404 and forward to error handler
app.use((req, res, next) => {
next(createError(404));
app.use((req, res) => {
return error(req, res, "Page not found", 404);
});
// error handler

View file

@ -1,8 +1,8 @@
let express = require("express");
let router = express.Router();
const sessionCheck = require("./utils/sessionCheck");
/* GET home page. */
router.get("/", (req, res) => {
router.get("/", sessionCheck(1), (req, res) => {
res.render("index", { title: "L'ETU" });
});

4
routes/utils/error.js Normal file
View file

@ -0,0 +1,4 @@
module.exports = (req, res, message, status, subMessage) => {
res.status(status || 500);
res.render("error", {message: message, error: {status: subMessage || undefined}});
};

View file

@ -0,0 +1,15 @@
let error = require("./error");
function sessionCheck(permission) {
return (req, res, next) => {
if (!req.session.user) {
req.session.lastUrl = req.originalUrl;
req.session.save(() => res.redirect("/login"));
} else if (req.session.user.permissions < permission)
return error(req, res, "Permission denied !", 403);
else
next();
}
}
module.exports = sessionCheck;