-
Notifications
You must be signed in to change notification settings - Fork 0
/
User.js
62 lines (57 loc) · 1.77 KB
/
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
50
51
52
53
54
55
56
57
58
59
60
61
62
import mongoose from 'mongoose';
import validator from 'validator';
import bcrypt from 'bcryptjs';
import jwt from 'jsonwebtoken';
const UserSchema = new mongoose.Schema({
name: {
type: String,
required: [true, 'Please provide name'],
trim: true,
minlength: [3, 'Name must be at least 3 characters'],
maxlength: [20, 'Name must be less than 20 characters'],
},
email: {
type: String,
required: [true, 'Please provide email'],
validate: {
validator: validator.isEmail,
message: 'Please provide valid email',
},
unique: true,
},
password: {
type: String,
required: [true, 'Please provide name'],
minlength: [6, 'password must be at least 6 characters'],
select: false,
},
lastName: {
type: String,
trim: true,
maxlength: [20, 'Lastname must be less than 20 characters'],
default: 'lastName',
},
skill: {
type: String,
trim: true,
maxlength: [20, 'Skill must be less than 20 characters'],
default: 'MERN',
},
});
// generate hash password
UserSchema.pre('save', async function () {
// console.log(this.modifiedPaths());
if (!this.isModified('password')) return;
const salt = await bcrypt.genSalt(10);
this.password = await bcrypt.hash(this.password, salt);
});
UserSchema.methods.createJWT = function () {
return jwt.sign({ userId: this._id }, process.env.JWT_SECRET, {
expiresIn: process.env.JWT_LIFETIME,
});
};
UserSchema.methods.comparePassword = async function (candidatePassword) {
const isMatch = await bcrypt.compare(candidatePassword, this.password);
return isMatch;
};
export default mongoose.model('User', UserSchema);