Notes/services/auth.js

52 lines
1.0 KiB
JavaScript
Raw Normal View History

2017-10-21 21:10:33 -04:00
"use strict";
2017-10-25 22:39:21 -04:00
const migration = require('./migration');
async function checkAuth(req, res, next) {
2017-10-15 16:32:49 -04:00
if (!req.session.loggedIn) {
res.redirect("login");
2017-10-25 22:39:21 -04:00
}
2017-10-29 14:55:48 -04:00
else if (await migration.isDbUpToDate()) {
2017-10-15 16:32:49 -04:00
next();
}
2017-10-25 22:39:21 -04:00
else {
res.redirect("migration");
}
2017-10-15 16:32:49 -04:00
}
async function checkAuthWithoutMigration(req, res, next) {
if (!req.session.loggedIn) {
res.redirect("login");
}
else {
next();
}
}
2017-10-25 22:39:21 -04:00
async function checkApiAuth(req, res, next) {
2017-10-28 22:17:00 -04:00
if (!req.session.loggedIn) {
2017-10-29 14:55:48 -04:00
res.status(401).send({});
2017-10-25 22:39:21 -04:00
}
2017-10-29 14:55:48 -04:00
else if (await migration.isDbUpToDate()) {
2017-10-15 16:32:49 -04:00
next();
}
2017-10-25 22:39:21 -04:00
else {
2017-10-29 14:55:48 -04:00
res.status(409).send({}); // need better response than that
2017-10-25 22:39:21 -04:00
}
2017-10-15 16:32:49 -04:00
}
async function checkApiAuthWithoutMigration(req, res, next) {
2017-10-28 22:17:00 -04:00
if (!req.session.loggedIn) {
2017-10-29 14:55:48 -04:00
res.status(401).send({});
}
else {
next();
}
}
2017-10-15 16:32:49 -04:00
module.exports = {
checkAuth,
checkAuthWithoutMigration,
checkApiAuth,
checkApiAuthWithoutMigration
2017-10-15 16:32:49 -04:00
};