From 136375cf11c66a40a7f3050ec1940615b6701e32 Mon Sep 17 00:00:00 2001 From: azivner Date: Wed, 27 Dec 2017 18:23:24 -0500 Subject: [PATCH] refactored detection of whether user is initialized --- services/auth.js | 9 ++------- services/sql.js | 11 ++++++++--- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/services/auth.js b/services/auth.js index 1531a5395..93b0d463f 100644 --- a/services/auth.js +++ b/services/auth.js @@ -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 { diff --git a/services/sql.js b/services/sql.js index 9b3108008..fa5d5c406 100644 --- a/services/sql.js +++ b/services/sql.js @@ -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,