-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathusers.js
48 lines (40 loc) · 900 Bytes
/
users.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
class User {
constructor(username, password) {
this._username = username;
this._password = password;
}
getUsername() {
return this._username;
}
getPassword() {
return this._password;
}
}
class Users {
constructor(mongo) {
this.db = mongo.db("app");
}
// This is just for demo purposes, normally you'd use bcrypt or something
async findBy(username, password) {
const collection = this.db.collection("users");
const user = await collection.findOne({
username: username,
password: password,
});
if (!user) {
return undefined;
}
return new User(user.username, user.password);
}
async persist(user) {
const collection = this.db.collection("users");
await collection.insertOne({
username: user.getUsername(),
password: user.getPassword(),
});
}
}
module.exports = {
Users,
User,
};