1
0
Fork 0
This repository has been archived on 2024-02-17. You can view files and clone it, but cannot push or open issues or pull requests.
letu/routes/utils/sessionCheck.js

18 lines
583 B
JavaScript
Raw Normal View History

2020-12-12 18:28:44 +01:00
const error = require("./error");
2020-10-12 23:34:51 +02:00
function sessionCheck(permission) {
return (req, res, next) => {
if (permission === -1 && req.session.user) {
2020-11-03 11:40:54 +01:00
res.redirect("/");
} else if (permission !== -1 && !req.session.user) {
2020-10-12 23:34:51 +02:00
req.session.lastUrl = req.originalUrl;
req.session.save(() => res.redirect("/login"));
} else if (req.session.user && req.session.user.permissions < permission) {
2020-10-12 23:34:51 +02:00
return error(req, res, "Permission denied !", 403);
} else
2020-10-12 23:34:51 +02:00
next();
}
}
module.exports = sessionCheck;