-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.js
78 lines (70 loc) · 1.88 KB
/
user.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
67
68
69
70
71
72
73
74
75
76
77
78
const bcrypt = require('bcrypt');
let users;
class User {
static async injectDB(conn) {
users = await conn.db("vms").collection("users")
}
/**
* @remarks
* This method is not implemented yet. To register a new user, you need to call this method.
*
* @param {*} username
* @param {*} password
* @param {*} phone
*/
static async register(username, password, email, role) {
// TODO: Check if username exists
let user = await users.findOne({ "username": username });
if (user) {
return null;
} else {
// TODO: Hash password
const hashpassword = await bcrypt.hash(password, 10);
// TODO: Save user to database
await users.insertOne({"username": username, "password": hashpassword, "email": email, "role": role});
}
return user = await users.findOne({ "username": username });
}
static async login(username, password) {
// TODO: Check if username exists
let user = await users.findOne({ "username": username });
if (!user) {
return null;
}
// TODO: Validate password
const match = await bcrypt.compare(password, user.password);
if (!match) {
return null;
}
// TODO: Return user object
return user;
}
static async update(username, email, role) {
let user = await users.findOne({ "username": username });
if (user) {
await users.updateOne({"username": username }, { $set: { "email": email, "role": role } });
return user = await users.findOne({ "username": username });
} else {
return null;
}
}
static async delete(username) {
let user = await users.findOne({ "username": username });
if (user) {
await users.deleteOne({ "username": username });
return true;
} else {
return null;
}
}
static async getAllUsers() {
let user = await users.find({ }).toArray();
if(user){
return user;
}
else {
return null
}
}
}
module.exports = User;