refactored detection of whether user is initialized

This commit is contained in:
azivner 2017-12-27 18:23:24 -05:00
parent eabc7f80b7
commit 136375cf11
2 changed files with 10 additions and 10 deletions

View file

@ -3,12 +3,9 @@
const migration = require('./migration');
const sql = require('./sql');
const utils = require('./utils');
const options = require('./options');
async function checkAuth(req, res, next) {
const username = await options.getOption('username');
if (!username) {
if (!await sql.isUserInitialized()) {
res.redirect("setup");
}
else if (!req.session.loggedIn && !utils.isElectron()) {
@ -53,9 +50,7 @@ async function checkApiAuthForMigrationPage(req, res, next) {
}
async function checkAppNotInitialized(req, res, next) {
const username = await options.getOption('username');
if (username) {
if (await sql.isUserInitialized()) {
res.status(400).send("App already initialized.");
}
else {

View file

@ -49,9 +49,7 @@ const dbReady = new Promise((resolve, reject) => {
// the database
}
else {
const username = await getFirstValue("SELECT opt_value FROM options WHERE opt_name = 'username'");
if (!username) {
if (!await isUserInitialized()) {
log.info("Login/password not initialized. DB not ready.");
return;
@ -235,8 +233,15 @@ async function isDbUpToDate() {
return upToDate;
}
async function isUserInitialized() {
const username = await getFirstValue("SELECT opt_value FROM options WHERE opt_name = 'username'");
return !!username;
}
module.exports = {
dbReady,
isUserInitialized,
insert,
replace,
getFirstValue,