-
Notifications
You must be signed in to change notification settings - Fork 0
/
usersdb.js
executable file
·71 lines (61 loc) · 1.48 KB
/
usersdb.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
const Users = require('./users');
const sqlite = require('sqlite3');
const DB = require('./db');
class UsersDb extends Users {
constructor() {
super();
this.db = new DB();
}
/**
*
* @param {string} name
* @param {string} password
* @returns {Promise}
*/
add(name, password) {
return new Promise((resolve, reject) => {
const user = {
name,
password,
sid: this.createSid()
};
this.db.forTable('Users').insert(user, (uid) => {
if (!uid) {
reject();
} else {
resolve(user);
}
});
});
}
/**
* get user by name
* @param {string} name
* @param {Function} callback
*/
get(name, callback) {
this.db.forTable('Users').filter({name}).select((result) => {
if (!result) {
callback(false);
return;
}
callback(result);
});
}
/**
* get current user by ssid
* @param {object} req
* @param {Function} callback
*/
getCurrent(req, callback) {
const sid = req.cookies.sid;
this.db.forTable('Users').filter({sid}).select((result) => {
if (!result) {
callback(false);
return;
}
callback(result);
});
}
}
module.exports = UsersDb;