-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.js
43 lines (37 loc) · 1.14 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
var db = require('./db.js'),
_db = db.db,
crypto = require('crypto');
function createTable() {
var stmt = 'CREATE TABLE IF NOT EXISTS users (' +
'id INTEGER PRIMARY KEY AUTOINCREMENT,' +
'email TEXT, ' +
'password TEXT' +
')';
db.buildTables(stmt);
}
function getUser(email, callback) {
var stmt = 'SELECT * FROM users WHERE email=$email;';
return _db.get(stmt, {$email: email}, callback);
}
exports.login = function (email, password, callback) {
getUser(email, function (err, row) {
var output = {success: false};
if (row && row.password === crypto.createHash('sha1').update(password).digest('base64')) {
output = row;
}
callback(err, output);
});
};
exports.create = function (user, callback) {
var stmt = 'INSERT INTO users VALUES (null, $email, $password);';
user.password = crypto.createHash('sha1').update(user.password).digest('base64');
getUser(user.email, function (err, row) {
var output = {success: false, msg: 'User already exists.', code: 409};
if (!row) {
_db.run(stmt, db.prepareFilter(user), callback);
return;
}
callback(err, output);
});
};
createTable();