forked from mdrcode/node-chat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.js
126 lines (105 loc) · 2.93 KB
/
models.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
var crypto = require('crypto');
function defineModels(mongoose, fn) {
var Schema = mongoose.Schema,
ObjectId = Schema.ObjectId;
/**
* User model
*
* Used for persisting users
*/
function validatePresenceOf(value) {
return value && value.length;
}
var User = new Schema({
email: { type: String, validate: [validatePresenceOf, 'Email address required'], index: { unique: true } },
name: String,
lastseen: Date,
isonline: Boolean,
hashed_password: String,
salt: String
});
User.virtual('lastseendate')
.get(function() {
return date.toReadableDate(this.lastseen, 'datestamp');
});
User.virtual('id')
.get(function() {
return this._id.toHexString();
});
User.virtual('password')
.set(function(pw) {
this._password = pw;
this.salt = this.createSalt();
this.hashed_password = this.encryptPassword(pw);
})
.get(function() { return this._password; });
User.method('authenticate', function(plain) {
return this.encryptPassword(plain) === this.hashed_password;
});
User.method('createSalt', function() {
return Math.round((new Date().valueOf() * Math.random())) + '';
});
User.method('encryptPassword', function(str) {
return crypto.createHmac('sha1', this.salt).update(str).digest('hex');
});
User.pre('save', function(next) {
if (!validatePresenceOf(this.hashed_password)) {
next(new Error('Invalid password'));
} else {
next();
}
});
//register validators
User.path('email').validate(function(val) {
return val.length > 0;
}, 'EMAIL_MISSING');
User.path('name').validate(function(val) {
return val.length > 0;
}, 'NAME_MISSING');
/**
* Message model
*
* Used for persisting chat messages
*/
var Message = new Schema({
posted: Date,
user: ObjectId,
message: String
});
Message.virtual('posteddate')
.get(function() {
return date.toReadableDate(this.posted, 'datestamp');
});
/**
* LoginToken model
*
* Used for persisting session tokens
*/
var LoginToken = new Schema({
email: { type: String, index: true },
series: { type: String, index: true },
token: { type: String, index: true }
});
LoginToken.virtual('id')
.get(function() {
return this._id.toHexString();
});
LoginToken.virtual('cookieValue')
.get(function() {
return JSON.stringify({ email: this.email, token: this.token, series: this.series });
});
LoginToken.method('randomToken', function() {
return Math.round((new Date().valueOf() * Math.random())) + '';
});
LoginToken.pre('save', function(next) {
this.token = this.randomToken();
this.series = this.randomToken();
next();
});
// register mongoose models
mongoose.model('Message', Message);
mongoose.model('User', User);
mongoose.model('LoginToken', LoginToken);
fn();
}
exports.defineModels = defineModels;