-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.js
More file actions
63 lines (57 loc) · 1.32 KB
/
user.js
File metadata and controls
63 lines (57 loc) · 1.32 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
'use strict';
var jwt = require('jsonwebtoken');
var config = require('../config/config');
var mongoose = require('mongoose');
// User Schema
var UserSchema = new mongoose.Schema({
username: String,
spritename: String,
facebookToken: String,
facebookId: String,
wechatToken: String,
wechatId: String,
strength: Number,
stamina: Number,
agility: Number,
health: Number,
healthLimit: Number,
experience: { type: Number, min: 0 },
appearance: String,
level: { type: Number, min: 0 },
skillInUse: [String],
updatedAt: Date,
createdAt: Date,
lastCombatTime: Date
});
UserSchema.methods.generateJwt = function() {
var expiry = new Date();
expiry.setDate(expiry.getDate() + 30); // 30 days
var token = jwt.sign({
_id: this._id,
name: this.username,
fbId: this.facebookId,
exp: parseInt(expiry.getTime() / 1000)
}, config.secret)
return token;
};
UserSchema.pre('save', function(next){
var now = new Date();
this.updatedAt = now;
if ( !this.createdAt ) {
this.createdAt = now;
}
next();
});
UserSchema.pre('update', function (next) {
this.update({}, {
updatedAt: Date.now()
});
next();
});
UserSchema.pre('findOneAndUpdate', function (next) {
this.update({}, {
updatedAt: Date.now()
});
next();
});
module.exports = mongoose.model('User', UserSchema);