@@ -4,10 +4,10 @@ import config from './load-config.js';
4
4
import * as uuid from 'uuid' ;
5
5
import * as bcrypt from 'bcrypt' ;
6
6
7
- let _accountDb = null ;
7
+ let _accountDb ;
8
8
9
9
export default function getAccountDb ( ) {
10
- if ( _accountDb == null ) {
10
+ if ( _accountDb === undefined ) {
11
11
const dbPath = join ( config . serverFiles , 'account.sqlite' ) ;
12
12
_accountDb = openDatabase ( dbPath ) ;
13
13
}
@@ -26,17 +26,17 @@ export function needsBootstrap() {
26
26
}
27
27
28
28
export function bootstrap ( password ) {
29
- let accountDb = getAccountDb ( ) ;
29
+ if ( password === undefined || password === '' ) {
30
+ return { error : 'invalid-password' } ;
31
+ }
30
32
33
+ let accountDb = getAccountDb ( ) ;
31
34
let rows = accountDb . all ( 'SELECT * FROM auth' ) ;
35
+
32
36
if ( rows . length !== 0 ) {
33
37
return { error : 'already-bootstrapped' } ;
34
38
}
35
39
36
- if ( password == null || password === '' ) {
37
- return { error : 'invalid-password' } ;
38
- }
39
-
40
40
// Hash the password. There's really not a strong need for this
41
41
// since this is a self-hosted instance owned by the user.
42
42
// However, just in case we do it.
@@ -45,6 +45,7 @@ export function bootstrap(password) {
45
45
46
46
let token = uuid . v4 ( ) ;
47
47
accountDb . mutate ( 'INSERT INTO sessions (token) VALUES (?)' , [ token ] ) ;
48
+
48
49
return { token } ;
49
50
}
50
51
@@ -58,31 +59,33 @@ export function login(password) {
58
59
59
60
let confirmed = row && bcrypt . compareSync ( password , row . password ) ;
60
61
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' } ;
70
64
}
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 } ;
71
72
}
72
73
73
74
export function changePassword ( newPassword ) {
74
- let accountDb = getAccountDb ( ) ;
75
-
76
- if ( newPassword == null || newPassword === '' ) {
75
+ if ( newPassword === undefined || newPassword === '' ) {
77
76
return { error : 'invalid-password' } ;
78
77
}
79
78
79
+ let accountDb = getAccountDb ( ) ;
80
+
80
81
let hashed = hashPassword ( newPassword ) ;
81
82
let token = uuid . v4 ( ) ;
83
+
82
84
// Note that this doesn't have a WHERE. This table only ever has 1
83
85
// row (maybe that will change in the future? if so this will not work)
84
86
accountDb . mutate ( 'UPDATE auth SET password = ?' , [ hashed ] ) ;
85
87
accountDb . mutate ( 'UPDATE sessions SET token = ?' , [ token ] ) ;
88
+
86
89
return { } ;
87
90
}
88
91
0 commit comments