-
Notifications
You must be signed in to change notification settings - Fork 0
/
userDao.js
47 lines (43 loc) · 1.21 KB
/
userDao.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
"use strict";
const { db } = require("./db");
const crypto = require("crypto");
exports.getUser = (email, password) => {
return new Promise((resolve, reject) => {
const sql = "SELECT * FROM user WHERE email = ?";
db.get(sql, [email], (err, row) => {
if (err) {
reject(err);
} else if (row === undefined) {
resolve(false);
} else {
const user = { id: row.id, username: row.email, name: row.name };
crypto.scrypt(password, row.salt, 32, function (err, hashedPassword) {
if (err) reject(err);
if (
!crypto.timingSafeEqual(
Buffer.from(row.password, "hex"),
hashedPassword
)
)
resolve(false);
else resolve(user);
});
}
});
});
};
exports.getUserById = (id) => {
return new Promise((resolve, reject) => {
const sql = "SELECT * FROM user WHERE id = ?";
db.get(sql, [id], (err, row) => {
if (err) {
reject(err);
} else if (row === undefined) {
resolve({ error: "User not found!" });
} else {
const user = { id: row.id, username: row.email, name: row.name };
resolve(user);
}
});
});
};