trilium/services/auth.js

72 lines
1.6 KiB
JavaScript
Raw Normal View History

2017-10-22 09:10:33 +08:00
"use strict";
2017-10-26 10:39:21 +08:00
const migration = require('./migration');
const sql = require('./sql');
const utils = require('./utils');
2017-12-04 11:29:23 +08:00
const options = require('./options');
2017-10-26 10:39:21 +08:00
async function checkAuth(req, res, next) {
2017-12-04 11:29:23 +08:00
const username = await options.getOption('username');
if (!username) {
res.redirect("setup");
}
else 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
}
else if (!await sql.isDbUpToDate()) {
2017-12-04 11:29:23 +08:00
res.redirect("migration");
2017-10-16 04:32:49 +08:00
}
2017-10-26 10:39:21 +08:00
else {
2017-12-04 11:29:23 +08:00
next();
2017-10-26 10:39:21 +08:00
}
2017-10-16 04:32:49 +08:00
}
async function checkAuthForMigrationPage(req, res, next) {
if (!req.session.loggedIn && !utils.isElectron()) {
res.redirect("login");
}
else {
next();
}
}
2017-10-26 10:39:21 +08:00
async function checkApiAuth(req, res, next) {
if (!req.session.loggedIn) {
2017-11-01 08:09:07 +08:00
res.status(401).send("Not authorized");
2017-10-26 10:39:21 +08:00
}
else if (await sql.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
}
async function checkApiAuthForMigrationPage(req, res, next) {
if (!req.session.loggedIn) {
2017-11-01 08:09:07 +08:00
res.status(401).send("Not authorized");
}
else {
next();
}
}
2017-12-04 11:29:23 +08:00
async function checkAppNotInitialized(req, res, next) {
const username = await options.getOption('username');
if (username) {
res.status(400).send("App already initialized.");
}
else {
next();
}
}
2017-10-16 04:32:49 +08:00
module.exports = {
checkAuth,
checkAuthForMigrationPage,
checkApiAuth,
2017-12-04 11:29:23 +08:00
checkApiAuthForMigrationPage,
checkAppNotInitialized
2017-10-16 04:32:49 +08:00
};