-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.js
More file actions
76 lines (56 loc) · 1.83 KB
/
models.js
File metadata and controls
76 lines (56 loc) · 1.83 KB
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
var crypto = require('crypto');
function defineModels(mongoose, callback) {
var Schema = mongoose.Schema
, ObjectId = Schema.ObjectId
;
// ----------------------------------------
// SAVED TEXT
// ----------------------------------------
SavedTextSchema = new Schema({
name: { type: String, required: true/*, index: { unique: true }*/ }
, description: String
, content: String
});
mongoose.model('SavedTextSchema', SavedTextSchema);
/*
// ----------------------------------------
// USER GROUP
// ----------------------------------------
UserGroupSchema = new Schema({
name: { type: String, required: true, index: { unique: true } }
, saved_texts: [SavedTextSchema]
});
mongoose.model('UserGroupSchema', UserGroupSchema);
*/
// ----------------------------------------
// USER
// ----------------------------------------
UserSchema = new Schema({
username: { type: String, required: true, index: { unique: true } }
, password_hashed: String
, first_name: String
, last_name: String
, salt: String
, texts: [SavedTextSchema]
// , '_group': { type: Schema.ObjectId, ref: 'UserGroupSchema' }
});
UserSchema.virtual('password')
.set(function(password) {
this._password = password;
this.salt = this.makeSalt();
this.password_hashed = this.encryptPassword(password);
})
.get(function() { return this._password; });
UserSchema.method('makeSalt', function() {
return Math.round((new Date().valueOf() * Math.random())) + '';
});
UserSchema.method('authenticate', function(pwdClear) {
return this.encryptPassword(pwdClear) === this.password_hashed;
});
UserSchema.method('encryptPassword', function(pwdClear) {
return crypto.createHmac('sha1', this.salt).update(pwdClear).digest('hex');
});
mongoose.model('UserSchema', UserSchema);
callback();
}
exports.defineModels = defineModels;