-
Notifications
You must be signed in to change notification settings - Fork 5
/
user-repository.js
66 lines (48 loc) · 1.35 KB
/
user-repository.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const fs = require('fs');
const path = require('path');
const bcrypt = require('bcrypt');
const { v4: uuidv4 } = require('uuid');
class Repository {
constructor() {
this.users = JSON.parse(fs.readFileSync(path.join(process.cwd(), 'users.json'), 'utf8'));
}
async create(name, password) {
this.users.push({ id: uuidv4(), name: name, password: bcrypt.hashSync(password, 10) });
this.save();
}
async findUserByNameAndPassword(name, password) {
const user = this.users.filter((user) => user.name === name)[0];
if (!user) {
return;
}
const match = await bcrypt.compare(password, user.password);
if (!match) {
return;
}
return user;
}
async findByName(name) {
const user = this.users.filter((user) => user.name === name)[0];
return user;
}
async findById(id) {
const user = this.users.filter((user) => user.id === id)[0];
return user;
}
async addPushVerification(id, factor) {
const user = this.users.filter((user) => user.id === id)[0];
if (!user) {
return;
}
this.users = this.users.map((user) => (user.id === id ? { ...user, factor: factor } : user));
await this.save();
}
async save() {
fs.writeFileSync(
path.join(process.cwd(), 'users.json'),
JSON.stringify(this.users, 0, 3),
'utf-8'
);
}
}
module.exports = Repository;