-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.js
47 lines (42 loc) · 1.19 KB
/
middleware.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
module.exports.isLoggedIn = (req, res, next) => {
if (!req.isAuthenticated()) {
req.session.redirectUrl = req.originalUrl;
req.flash("error", "You must be logged in to access that!");
return res.redirect("/login");
}
next();
};
module.exports.saveRedirectUrl = (req, res, next) => {
if (req.session.redirectUrl) {
res.locals.redirectUrl = req.session.redirectUrl;
}
next();
};
module.exports.isTeacher = (req, res, next) => {
if (req.user.role !== "teacher") {
req.flash("error", "Only teachers can access that!");
return res.redirect("/");
}
next();
};
module.exports.isStudent = (req, res, next) => {
if (req.user.role !== "student") {
req.flash("error", "Only students can access that!");
return res.redirect("/");
}
next();
};
module.exports.isAdmin = (req, res, next) => {
if (req.user.role !== "admin") {
req.flash("error", "Only admins can access that!");
return res.redirect("/");
}
next();
};
module.exports.isTeacherOrAdmin = (req, res, next) => {
if (req.user.role !== "admin" && req.user.role !== "teacher") {
req.flash("error", "Only teachers and admins can access that!");
return res.redirect("/");
}
next();
};