-
Notifications
You must be signed in to change notification settings - Fork 4
/
log.user.model.js
123 lines (109 loc) · 3.1 KB
/
log.user.model.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
/*Copyright 2016 Wipro Limited, NIIT Limited
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
This code is written by Prateek Reddy Yammanuru, Shiva Manognya Kandikuppa, Uday Kumar Mydam, Nirup TNL, Sandeep Reddy G, Deepak Kumar*/
var mongoose = require('mongoose');
var crypto = require('crypto');
var Schema = mongoose.Schema;
var LogUserSchema = new Schema({
firstName : String,
lastName : String,
email: {
type: String,
match: [/.+\@.+\..+/, "Please fill a valid e-mail address"]
},
username : {
type: String,
unique: true,
required: 'Username is required',
trim: true
},
google : {
id: String,
token: String,
email: String,
name: String
},
facebook : {
id: String,
token: String,
email: String,
name: String
},
password : {
type: String,
validate: [
function(password) {
return password && password.length > 6;
}, 'Password should be longer'
]
},
// type : String,
hash : String,
// provider: {
// type: String,
// required: 'Provider is required'
// },
// providerId: String,
// providerData: {},
organization : {
type: String,
required: 'Organization is required',
trim: true
},
created: {
type: Date,
default: Date.now
}
},{collection: "users"});
LogUserSchema.virtual('fullName').get(function() {
return this.firstName + ' ' + this.lastName;
}).set(function(fullName) {
var splitName = fullName.split(' ');
this.firstName = splitName[0] || '';
this.lastName = splitName[1] || '';
});
// LogUserSchema.pre('save', function(next) {
// if (this.password) {
// this.hash = new Buffer(crypto.randomBytes(16).toString('base64'), 'base64');
// this.password = this.hashPassword(this.password);
// }
// next();
// });
LogUserSchema.methods.hashPassword = function(password) {
return crypto.pbkdf2Sync(password, this.hash, 10000, 64).toString('base64');
};
LogUserSchema.methods.authenticate = function(password) {
console.log("inside log user model");
return this.password === this.hashPassword(password);
};
LogUserSchema.statics.findUniqueUsername = function(username, suffix, callback) {
var _this = this;
var possibleUsername = username + (suffix || '');
_this.findOne({
username: possibleUsername
}, function(err, user) {
if (!err) {
if (!user) {
callback(possibleUsername);
} else {
return _this.findUniqueUsername(username, (suffix || 0) + 1, callback);
}
} else {
callback(null);
}
});
};
LogUserSchema.set('toJSON', {
getters: true,
virtuals: true
});
//mongoose.model('User', LogUserSchema);
module.exports = LogUserSchema;