2017-10-22 09:10:33 +08:00
|
|
|
"use strict";
|
|
|
|
|
2017-10-26 10:39:21 +08:00
|
|
|
const migration = require('./migration');
|
2017-11-06 06:58:55 +08:00
|
|
|
const utils = require('./utils');
|
2017-10-26 10:39:21 +08:00
|
|
|
|
|
|
|
async function checkAuth(req, res, next) {
|
2017-11-06 06:58:55 +08:00
|
|
|
if (!req.session.loggedIn && !utils.isElectron()) {
|
2017-10-16 04:32:49 +08:00
|
|
|
res.redirect("login");
|
2017-10-26 10:39:21 +08:00
|
|
|
}
|
2017-10-30 02:55:48 +08:00
|
|
|
else if (await migration.isDbUpToDate()) {
|
2017-10-16 04:32:49 +08:00
|
|
|
next();
|
|
|
|
}
|
2017-10-26 10:39:21 +08:00
|
|
|
else {
|
|
|
|
res.redirect("migration");
|
|
|
|
}
|
2017-10-16 04:32:49 +08:00
|
|
|
}
|
|
|
|
|
2017-10-27 08:31:31 +08:00
|
|
|
async function checkAuthWithoutMigration(req, res, next) {
|
2017-11-06 06:58:55 +08:00
|
|
|
if (!req.session.loggedIn && !utils.isElectron()) {
|
2017-10-27 08:31:31 +08:00
|
|
|
res.redirect("login");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-26 10:39:21 +08:00
|
|
|
async function checkApiAuth(req, res, next) {
|
2017-11-06 06:58:55 +08:00
|
|
|
if (!req.session.loggedIn && !utils.isElectron()) {
|
2017-11-01 08:09:07 +08:00
|
|
|
res.status(401).send("Not authorized");
|
2017-10-26 10:39:21 +08:00
|
|
|
}
|
2017-10-30 02:55:48 +08:00
|
|
|
else if (await migration.isDbUpToDate()) {
|
2017-10-16 04:32:49 +08:00
|
|
|
next();
|
|
|
|
}
|
2017-10-26 10:39:21 +08:00
|
|
|
else {
|
2017-11-01 08:09:07 +08:00
|
|
|
res.status(409).send("Mismatched app versions"); // need better response than that
|
2017-10-26 10:39:21 +08:00
|
|
|
}
|
2017-10-16 04:32:49 +08:00
|
|
|
}
|
|
|
|
|
2017-10-27 08:31:31 +08:00
|
|
|
async function checkApiAuthWithoutMigration(req, res, next) {
|
2017-11-06 06:58:55 +08:00
|
|
|
if (!req.session.loggedIn && !utils.isElectron()) {
|
2017-11-01 08:09:07 +08:00
|
|
|
res.status(401).send("Not authorized");
|
2017-10-27 08:31:31 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-16 04:32:49 +08:00
|
|
|
module.exports = {
|
|
|
|
checkAuth,
|
2017-10-27 08:31:31 +08:00
|
|
|
checkAuthWithoutMigration,
|
|
|
|
checkApiAuth,
|
|
|
|
checkApiAuthWithoutMigration
|
2017-10-16 04:32:49 +08:00
|
|
|
};
|