Skip to content

A local-first personal finance app

Refactor to add strict type comparisons and remove unnecessary else blocks (#343)

Matt Fiddaman4ae654d54da1
diff --git a/src/account-db.js b/src/account-db.js
index 52958e3148..4d5c4892b3 100644
--- a/src/account-db.js
+++ b/src/account-db.js
@@ -4,10 +4,10 @@ import config from './load-config.js';
import * as uuid from 'uuid';
import * as bcrypt from 'bcrypt';
-let _accountDb = null;
+let _accountDb;
export default function getAccountDb() {
- if (_accountDb == null) {
+ if (_accountDb === undefined) {
const dbPath = join(config.serverFiles, 'account.sqlite');
_accountDb = openDatabase(dbPath);
}
@@ -26,17 +26,17 @@ export function needsBootstrap() {
}
export function bootstrap(password) {
- let accountDb = getAccountDb();
+ if (password === undefined || password === '') {
+ return { error: 'invalid-password' };
+ }
+ let accountDb = getAccountDb();
let rows = accountDb.all('SELECT * FROM auth');
+
if (rows.length !== 0) {
return { error: 'already-bootstrapped' };
}
- if (password == null || password === '') {
- return { error: 'invalid-password' };
- }
-
// Hash the password. There's really not a strong need for this
// since this is a self-hosted instance owned by the user.
// However, just in case we do it.
@@ -45,6 +45,7 @@ export function bootstrap(password) {
let token = uuid.v4();
accountDb.mutate('INSERT INTO sessions (token) VALUES (?)', [token]);
+
return { token };
}
@@ -58,31 +59,33 @@ export function login(password) {
let confirmed = row && bcrypt.compareSync(password, row.password);
- if (confirmed) {
- // Right now, tokens are permanent and there's just one in the
- // system. In the future this should probably evolve to be a
- // "session" that times out after a long time or something, and
- // maybe each device has a different token
- let row = accountDb.first('SELECT * FROM sessions');
- return { token: row.token };
- } else {
- return null;
+ if (!confirmed) {
+ return { error: 'invalid-password' };
}
+
+ // Right now, tokens are permanent and there's just one in the
+ // system. In the future this should probably evolve to be a
+ // "session" that times out after a long time or something, and
+ // maybe each device has a different token
+ let sessionRow = accountDb.first('SELECT * FROM sessions');
+ return { token: sessionRow.token };
}
export function changePassword(newPassword) {
- let accountDb = getAccountDb();
-
- if (newPassword == null || newPassword === '') {
+ if (newPassword === undefined || newPassword === '') {
return { error: 'invalid-password' };
}
+ let accountDb = getAccountDb();
+
let hashed = hashPassword(newPassword);
let token = uuid.v4();
+
// Note that this doesn't have a WHERE. This table only ever has 1
// row (maybe that will change in the future? if so this will not work)
accountDb.mutate('UPDATE auth SET password = ?', [hashed]);
accountDb.mutate('UPDATE sessions SET token = ?', [token]);
+
return {};
}
diff --git a/src/app-account.js b/src/app-account.js
index 0f57e23ac7..d26babdb09 100644
--- a/src/app-account.js
+++ b/src/app-account.js
@@ -32,9 +32,9 @@ app.post('/bootstrap', (req, res) => {
if (error) {
res.status(400).send({ status: 'error', reason: error });
return;
- } else {
- res.send({ status: 'ok', data: { token } });
}
+
+ res.send({ status: 'ok', data: { token } });
});
app.post('/login', (req, res) => {
diff --git a/upcoming-release-notes/343.md b/upcoming-release-notes/343.md
new file mode 100644
index 0000000000..fe624c1378
--- /dev/null
+++ b/upcoming-release-notes/343.md
@@ -0,0 +1,6 @@
+---
+category: Maintenance
+authors: [matt-fidd]
+---
+
+Refactor to add strict type comparisons and remove unnecessary else blocks