1
+ import { passwordIterations } from '../../config' ;
2
+ import { createHash , createSalt } from '../../helpers/hash-password' ;
3
+ import { cache } from '../cache' ;
1
4
import * as DB from '../database' ;
2
5
import * as HNDB from '../hn-data-api' ;
3
- import { cache } from '../cache' ;
4
- import { createHash , createSalt } from '../../helpers/hash-password' ;
5
- import { passwordIterations } from '../../config' ;
6
- import { isValidUser , isValidNewUser } from '../validation/user' ;
6
+ import { isValidNewUser , isValidUser } from '../validation/user' ;
7
7
8
8
export class User {
9
9
public readonly id : string ;
@@ -21,7 +21,9 @@ export class User {
21
21
public readonly passwordSalt : string ;
22
22
23
23
constructor ( props ) {
24
- if ( ! props . id ) throw new Error ( `Error instantiating User, id invalid: ${ props . id } ` ) ;
24
+ if ( ! props . id ) {
25
+ throw new Error ( `Error instantiating User, id invalid: ${ props . id } ` ) ;
26
+ }
25
27
isValidUser ( props ) ;
26
28
27
29
this . id = props . id ;
@@ -45,7 +47,10 @@ export class User {
45
47
46
48
static validPassword = async ( id , password ) => {
47
49
const user = cache . getUser ( id ) ;
48
- if ( user ) return ( await createHash ( password , user . passwordSalt , passwordIterations ) ) === user . hashedPassword ;
50
+ if ( user ) {
51
+ return ( await createHash ( password , user . passwordSalt , passwordIterations ) ) === user . hashedPassword ;
52
+ }
53
+
49
54
return false ;
50
55
} ;
51
56
@@ -54,15 +59,17 @@ export class User {
54
59
isValidNewUser ( user ) ;
55
60
56
61
// Check if user already exists
57
- if ( cache . getUser ( user . id ) ) throw new Error ( 'Username is taken.' ) ;
62
+ if ( cache . getUser ( user . id ) ) {
63
+ throw new Error ( 'Username is taken.' ) ;
64
+ }
58
65
59
66
// Go ahead and create the new user
60
67
const passwordSalt = createSalt ( ) ;
61
68
const hashedPassword = await createHash ( user . password , passwordSalt , passwordIterations ) ;
62
69
63
70
const newUser = new User ( {
64
- id : user . id ,
65
71
hashedPassword,
72
+ id : user . id ,
66
73
passwordSalt,
67
74
} ) ;
68
75
0 commit comments