-
Notifications
You must be signed in to change notification settings - Fork 3
/
user.js
49 lines (41 loc) · 976 Bytes
/
user.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
import mongoose from 'mongoose';
import bcrypt from 'bcrypt-nodejs';
var UserSchema = new mongoose.Schema({
name: {
type: String,
unique: true,
required: true
},
email : {
type: String,
unique: true,
required: true
},
password: {
type: String,
required: true
}
});
UserSchema.pre('save', (callback) => {
var user = this; // is undefined
console.log(this);
if(!user.isModified('password'))
return callback();
bcrypt.genSalt(5, (err, salt) => {
if (err)
return callback(err);
bcrypt.hash(user.password, salt, null, (err,hash) => {
if (err)
return callback(err);
user.password = hash;
callback();
});
});
});
UserSchema.methods.verifyPassword = (password, cd) => {
bcrypt.compare(password, this.password, (err, isMatch) => {
if (err) return cb(err);
cd(null, isMatch);
});
};
module.exports = mongoose.model("User", UserSchema);