-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathlocal-db.js
More file actions
43 lines (35 loc) · 1014 Bytes
/
local-db.js
File metadata and controls
43 lines (35 loc) · 1014 Bytes
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
const low = require('lowdb');
const crypt = require('./node-crypt3/sync');
const FileSync = require('lowdb/adapters/FileSync');
const adapter = new FileSync('db.json');
const db = low(adapter);
class LocalDB {
constructor() {
this.db = db;
}
init() {
db.defaults({
users: {}
}).write();
}
getUser(username) {
return db.get('users').value()[username.toLowerCase()];
}
isRegistered(username) {
return this.getUser(username) ? true : false;
}
isValidLogin(username, password) {
let user = this.getUser(username);
return user && crypt(password, user.password) === user.password;
}
register(data) {
db.get('users').assign({
[data.username.toLowerCase()]: {
username: data.username,
password: crypt(data.password, data.password),
email: data.email
}
}).write();
}
}
exports = module.exports = new LocalDB();