Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
refactored detection of whether user is initialized
azivner136375cf11c6
diff --git a/services/auth.js b/services/auth.jsindex 1531a5395a..93b0d463f3 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.jsindex 9b3108008a..fa5d5c4063 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,