Skip to content
This repository was archived by the owner on Feb 10, 2025. It is now read-only.

Commit 4ae654d

Browse files
authored
Refactor to add strict type comparisons and remove unnecessary else blocks (#343)
1 parent 1bbba66 commit 4ae654d

File tree

3 files changed

+30
-21
lines changed

3 files changed

+30
-21
lines changed

src/account-db.js

+22-19
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import config from './load-config.js';
44
import * as uuid from 'uuid';
55
import * as bcrypt from 'bcrypt';
66

7-
let _accountDb = null;
7+
let _accountDb;
88

99
export default function getAccountDb() {
10-
if (_accountDb == null) {
10+
if (_accountDb === undefined) {
1111
const dbPath = join(config.serverFiles, 'account.sqlite');
1212
_accountDb = openDatabase(dbPath);
1313
}
@@ -26,17 +26,17 @@ export function needsBootstrap() {
2626
}
2727

2828
export function bootstrap(password) {
29-
let accountDb = getAccountDb();
29+
if (password === undefined || password === '') {
30+
return { error: 'invalid-password' };
31+
}
3032

33+
let accountDb = getAccountDb();
3134
let rows = accountDb.all('SELECT * FROM auth');
35+
3236
if (rows.length !== 0) {
3337
return { error: 'already-bootstrapped' };
3438
}
3539

36-
if (password == null || password === '') {
37-
return { error: 'invalid-password' };
38-
}
39-
4040
// Hash the password. There's really not a strong need for this
4141
// since this is a self-hosted instance owned by the user.
4242
// However, just in case we do it.
@@ -45,6 +45,7 @@ export function bootstrap(password) {
4545

4646
let token = uuid.v4();
4747
accountDb.mutate('INSERT INTO sessions (token) VALUES (?)', [token]);
48+
4849
return { token };
4950
}
5051

@@ -58,31 +59,33 @@ export function login(password) {
5859

5960
let confirmed = row && bcrypt.compareSync(password, row.password);
6061

61-
if (confirmed) {
62-
// Right now, tokens are permanent and there's just one in the
63-
// system. In the future this should probably evolve to be a
64-
// "session" that times out after a long time or something, and
65-
// maybe each device has a different token
66-
let row = accountDb.first('SELECT * FROM sessions');
67-
return { token: row.token };
68-
} else {
69-
return null;
62+
if (!confirmed) {
63+
return { error: 'invalid-password' };
7064
}
65+
66+
// Right now, tokens are permanent and there's just one in the
67+
// system. In the future this should probably evolve to be a
68+
// "session" that times out after a long time or something, and
69+
// maybe each device has a different token
70+
let sessionRow = accountDb.first('SELECT * FROM sessions');
71+
return { token: sessionRow.token };
7172
}
7273

7374
export function changePassword(newPassword) {
74-
let accountDb = getAccountDb();
75-
76-
if (newPassword == null || newPassword === '') {
75+
if (newPassword === undefined || newPassword === '') {
7776
return { error: 'invalid-password' };
7877
}
7978

79+
let accountDb = getAccountDb();
80+
8081
let hashed = hashPassword(newPassword);
8182
let token = uuid.v4();
83+
8284
// Note that this doesn't have a WHERE. This table only ever has 1
8385
// row (maybe that will change in the future? if so this will not work)
8486
accountDb.mutate('UPDATE auth SET password = ?', [hashed]);
8587
accountDb.mutate('UPDATE sessions SET token = ?', [token]);
88+
8689
return {};
8790
}
8891

src/app-account.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ app.post('/bootstrap', (req, res) => {
3232
if (error) {
3333
res.status(400).send({ status: 'error', reason: error });
3434
return;
35-
} else {
36-
res.send({ status: 'ok', data: { token } });
3735
}
36+
37+
res.send({ status: 'ok', data: { token } });
3838
});
3939

4040
app.post('/login', (req, res) => {

upcoming-release-notes/343.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
category: Maintenance
3+
authors: [matt-fidd]
4+
---
5+
6+
Refactor to add strict type comparisons and remove unnecessary else blocks

0 commit comments

Comments
 (0)