-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuserModel.js
More file actions
59 lines (48 loc) · 1.55 KB
/
userModel.js
File metadata and controls
59 lines (48 loc) · 1.55 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
const mongoose = require('mongoose')
const bcrypt = require('bcryptjs')
const jwt = require('jsonwebtoken')
const userSchema = new mongoose.Schema({
role: {
type: String,
enum: ["customer", "admin"],
default: "customer",
},
name: {
type: String,
required: [true, "Please Enter Your Name"],
trim: true,
},
email: {
type: String,
required: [true, "Please Enter Your Email"],
unique: true,
trim: true,
match: [/.+\@.+\..+/, "Please enter valid email address"] // Characters checks the user added the valid email containing . or @ etc
},
password: {
type: String,
required: true,
minLength: 6,
},
},
{ timestamps: true }, // Its auto add createdAt and updatedAt
)
// Password Hash Middleware
userSchema.pre("save", async function (next) {
// Salt: A salt adds random string to the password making it unique so if the pass is same but the hash will never be due to this string.
if (!this.isModified("password")) return next();
const salt = await bcrypt.genSalt(10)
this.password = await bcrypt.hash(this.password, salt);
next();
})
// Compare Password
userSchema.methods.comparePassword = async function (enteredPassword) {
return await bcrypt.compare(enteredPassword, this.password)
}
// JWT Token
userSchema.methods.getJWTToken = function () {
return jwt.sign({ id: this._id }, process.env.JWT_SECRET, {
expiresIn: process.env.JWT_EXPIRE
})
}
module.exports = mongoose.model("User", userSchema)