User.js
3 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
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
var mongoose = require('mongoose');
var bcrypt = require('bcryptjs');
// schema
var userSchema = mongoose.Schema({
username:{
type:String,
required:[true,'Username is required!'],
match:[/^.{4,12}$/,'Should be 4-12 characters!'],
trim:true,
unique:true
},
password:{
type:String,
required:[true,'Password is required!'],
select:false
},
name:{
type:String,
required:[true,'Name is required!'],
match:[/^.{4,12}$/,'Should be 4-12 characters!'],
trim:true
},
email:{
type:String,
match:[/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/,'Should be a vaild email address!'],
trim:true
}
},{
toObject:{virtuals:true}
});
// virtuals
userSchema.virtual('passwordConfirmation')
.get(function(){ return this._passwordConfirmation; })
.set(function(value){ this._passwordConfirmation=value; });
userSchema.virtual('originalPassword')
.get(function(){ return this._originalPassword; })
.set(function(value){ this._originalPassword=value; });
userSchema.virtual('currentPassword')
.get(function(){ return this._currentPassword; })
.set(function(value){ this._currentPassword=value; });
userSchema.virtual('newPassword')
.get(function(){ return this._newPassword; })
.set(function(value){ this._newPassword=value; });
// password validation
var passwordRegex = /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,16}$/;
var passwordRegexErrorMessage = 'Should be minimum 8 characters of alphabet and number combination!';
userSchema.path('password').validate(function(v) {
var user = this;
// create user
if(user.isNew){
if(!user.passwordConfirmation){
user.invalidate('passwordConfirmation', 'Password Confirmation is required.');
}
if(!passwordRegex.test(user.password)){
user.invalidate('password', passwordRegexErrorMessage);
}
else if(user.password !== user.passwordConfirmation) {
user.invalidate('passwordConfirmation', 'Password Confirmation does not matched!');
}
}
// update user
if(!user.isNew){
if(!user.currentPassword){
user.invalidate('currentPassword', 'Current Password is required!');
}
else if(!bcrypt.compareSync(user.currentPassword, user.originalPassword)){
user.invalidate('currentPassword', 'Current Password is invalid!');
}
if(user.newPassword && !passwordRegex.test(user.newPassword)){
user.invalidate("newPassword", passwordRegexErrorMessage);
}
else if(user.newPassword !== user.passwordConfirmation) {
user.invalidate('passwordConfirmation', 'Password Confirmation does not matched!');
}
}
});
// hash password
userSchema.pre('save', function (next){
var user = this;
if(!user.isModified('password')){
return next();
}
else {
user.password = bcrypt.hashSync(user.password);
return next();
}
});
// model methods
userSchema.methods.authenticate = function (password) {
var user = this;
return bcrypt.compareSync(password,user.password);
};
// model & export
var User = mongoose.model('user',userSchema);
module.exports = User;