-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.js
38 lines (34 loc) · 1022 Bytes
/
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
// since app is not defined in this file, I cannot perform app methods.
// app is defined in server.js, it is a big JS object.
// it can be exported and required!
const { app } = require("./server");
module.exports.requireLoggedOutUser = function (req, res, next) {
if (req.session.userId) {
app.locals.loggedIn = true;
return res.redirect("/petition");
}
next();
};
module.exports.requireLoggedInUser = function (req, res, next) {
if (!req.session.userId) {
app.locals.loggedIn = false;
return res.redirect("/register");
}
if (req.session.userId) {
app.locals.loggedIn = true;
}
next();
};
module.exports.requireSignature = function (req, res, next) {
if (!req.session.signatureId) {
return res.redirect("/petition");
}
next();
};
module.exports.requireNoSignature = function (req, res, next) {
if (req.session.signatureId) {
app.locals.signed = true;
return res.redirect("/thanks");
}
next();
};