From 5887f8c14cffcad558e0f336ad637116fba722a7 Mon Sep 17 00:00:00 2001 From: Beaudan Date: Fri, 5 Jul 2019 13:21:32 +1000 Subject: [PATCH] Fix password error --- app/sql.js | 5 ++++- main.js | 15 ++++++++++----- password_preload.js | 8 ++++---- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/app/sql.js b/app/sql.js index ef875299e..ac5d5ec2c 100644 --- a/app/sql.js +++ b/app/sql.js @@ -785,7 +785,7 @@ function _initializePaths(configDir) { filePath = path.join(dbDir, 'db.sqlite'); } -async function initialize({ configDir, key, messages }) { +async function initialize({ configDir, key, messages, passwordAttempt }) { if (db) { throw new Error('Cannot initialize more than once!'); } @@ -828,6 +828,9 @@ async function initialize({ configDir, key, messages }) { // test database await getMessageCount(); } catch (error) { + if (passwordAttempt) { + throw error; + } console.log('Database startup error:', error.stack); const buttonIndex = dialog.showMessageBox({ buttons: [ diff --git a/main.js b/main.js index c2135252d..51e380d0e 100644 --- a/main.js +++ b/main.js @@ -746,10 +746,11 @@ app.on('ready', async () => { // Try to show the main window with the default key // If that fails then show the password window - try { - await showMainWindow(key); - } catch (e) { + const passwordSet = userConfig.get('passwordSet'); + if (passwordSet) { showPasswordWindow(); + } else { + await showMainWindow(key); } }); @@ -779,13 +780,14 @@ async function removeDB() { } } -async function showMainWindow(sqlKey) { +async function showMainWindow(sqlKey, passwordAttempt = false) { const userDataPath = await getRealPath(app.getPath('userData')); await sql.initialize({ configDir: userDataPath, key: sqlKey, messages: locale.messages, + passwordAttempt, }); await sqlChannels.initialize(); @@ -1009,7 +1011,8 @@ ipc.on('password-window-login', async (event, passPhrase) => { event.sender.send('password-window-login-response', e); try { - await showMainWindow(passPhrase); + const passwordAttempt = true; + await showMainWindow(passPhrase, passwordAttempt); sendResponse(); if (passwordWindow) { passwordWindow.close(); @@ -1041,10 +1044,12 @@ ipc.on('set-password', async (event, passPhrase, oldPhrase) => { const defaultKey = getDefaultSQLKey(); await sql.setSQLPassword(defaultKey); await sql.removePasswordHash(); + userConfig.set('passwordSet', false); } else { await sql.setSQLPassword(passPhrase); const newHash = passwordUtil.generateHash(passPhrase); await sql.savePasswordHash(newHash); + userConfig.set('passwordSet', true); } sendResponse(); diff --git a/password_preload.js b/password_preload.js index 9253aa285..8d8a5b7de 100644 --- a/password_preload.js +++ b/password_preload.js @@ -11,6 +11,10 @@ const localeMessages = ipcRenderer.sendSync('locale-data'); window.theme = config.theme; window.i18n = i18n.setup(locale, localeMessages); +window.getEnvironment = () => config.environment; +window.getVersion = () => config.version; +window.getAppInstance = () => config.appInstance; + // So far we're only using this for Signal.Types const Signal = require('./js/modules/signal'); @@ -20,10 +24,6 @@ window.Signal = Signal.setup({ getRegionCode: () => null, }); -window.getEnvironment = () => config.environment; -window.getVersion = () => config.version; -window.getAppInstance = () => config.appInstance; - window.passwordUtil = require('./app/password_util'); window.resetDatabase = () => {