22
33import type { TokenObject , User , UserCredentials } from '../types' ;
44
5- import { JSONFileManager , uuid } from 'spark-protocol' ;
5+ import uuid from 'uuid' ;
6+ import { JSONFileManager } from 'spark-protocol' ;
67import PasswordHasher from '../lib/PasswordHasher' ;
78import HttpError from '../lib/HttpError' ;
89
@@ -20,12 +21,16 @@ class UserFileRepository {
2021
2122 const salt = await PasswordHasher . generateSalt ( ) ;
2223 const passwordHash = await PasswordHasher . hash ( password , salt ) ;
24+ let id = uuid ( ) ;
25+ while ( await this . getById ( id ) ) {
26+ id = uuid ( ) ;
27+ }
2328
2429 const modelToSave = {
2530 accessTokens : [ ] ,
2631 created_at : new Date ( ) ,
2732 created_by : null ,
28- id : uuid ( ) ,
33+ id,
2934 passwordHash,
3035 salt,
3136 username,
@@ -43,10 +48,10 @@ class UserFileRepository {
4348 throw new HttpError ( 'Not implemented' ) ;
4449 } ;
4550
46- getAll = ( ) : Promise < Array < User >> =>
51+ getAll = async ( ) : Promise < Array < User >> =>
4752 this . _fileManager . getAllData ( ) ;
4853
49- getById = ( id : string ) : Promise < ?User > =>
54+ getById = async ( id : string ) : Promise < ?User > =>
5055 this . _fileManager . getFile ( `${ id } .json` ) ;
5156
5257 getByUsername = async ( username : string ) : Promise < ?User > =>
@@ -91,7 +96,7 @@ class UserFileRepository {
9196 this . _fileManager . writeFile ( `${ user . id } .json` , userToSave ) ;
9297 } ;
9398
94- deleteById = ( id : string ) : Promise < void > =>
99+ deleteById = async ( id : string ) : Promise < void > =>
95100 this . _fileManager . deleteFile ( `${ id } .json` ) ;
96101
97102
0 commit comments