-
Notifications
You must be signed in to change notification settings - Fork 5
/
users.js
45 lines (33 loc) · 878 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
'use strict';
var jwt = require('jsonwebtoken');
function User(username, password, id) {
this.username = username;
this.password = password;
this.id = id;
}
User.prototype.generateToken = function(secret) {
// set expiration to 1 day
var today = new Date();
var exp = new Date(today);
exp.setDate(today.getDate() + 1);
return jwt.sign({
username: this.username,
id: this.id,
exp: parseInt(exp.getTime() / 1000)
}, secret);
};
User.prototype.validPassword = function(password) {
return this.password === password;
};
var users = [];
// users.push(new User('matt', 'test', 0));
exports.find = function(username) {
return users.find(function(u, i) {
return u.username === username;
});
};
exports.add = function(username, password) {
var user = new User(username, password, users.length);
users.push(user);
return user;
};