-
Notifications
You must be signed in to change notification settings - Fork 11
/
user-dao.js
46 lines (42 loc) · 1.42 KB
/
user-dao.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
'use strict';
/* Data Access Object (DAO) module for accessing users */
const db = require('./db');
const bcrypt = require('bcrypt');
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 {
// by default, the local strategy looks for "username": not to create confusion in server.js, we can create an object with that property
const user = {id: row.id, username: row.email, name: row.name}
resolve(user);
}
});
});
};
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};
// check the hashes with an async call, given that the operation may be CPU-intensive (and we don't want to block the server)
bcrypt.compare(password, row.password).then(result => {
if(result)
resolve(user);
else
resolve(false);
});
}
});
});
};