-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.js
More file actions
29 lines (25 loc) · 706 Bytes
/
Copy pathuser.js
File metadata and controls
29 lines (25 loc) · 706 Bytes
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
import mongoose, { Schema } from 'mongoose';
import bcrypt from 'bcryptjs';
const schema = new Schema(
{
_id: { type: String },
name: { type: String },
email: { type: String },
facebookId: { type: String },
password: { type: String },
status: { type: String }
}, {
timestamps: true
}
);
schema.pre('save', function (next) {
const user = this;
if (!user.isModified('password')) return next();
user.password = bcrypt.hashSync(this.password, 12);
return next();
});
schema.methods.validatePassword = function (candidatePassword) {
return bcrypt.compareSync(candidatePassword, this.password);
};
const User = mongoose.model('user', schema);
export default User;