-
Notifications
You must be signed in to change notification settings - Fork 0
/
userModel.js
69 lines (65 loc) · 1.7 KB
/
userModel.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
63
64
65
66
67
68
69
const mongoose = require('mongoose'); // Erase if already required
const bcrypt = require("bcrypt");
/**(node:16668) [MONGOOSE] DeprecationWarning: Mongoose: the `strictQuery` option will be switched back to `false` by default in Mongoose 7.
* Use `mongoose.set('strictQuery', false);` if you want to prepare for this change.
* Or use `mongoose.set('strictQuery', true);` to suppress this warning. */
mongoose.set('strictQuery', false);
// Declare the Schema of the Mongo model
var userSchema = new mongoose.Schema({
firstname: {
type: String,
required: true,
},
lastname: {
type: String,
required: true,
},
email: {
type: String,
required: true,
unique: true,
},
mobile: {
type: String,
required: true,
unique: true,
},
password: {
type: String,
required: true,
},
role: {
type: String,
default: "user",
},
isBlocked: {
type: Boolean,
default: false,
},
cart: {
type: Array,
default: [],
},
address: [{
type: mongoose.Schema.Types.ObjectId,
ref: "Address",
}],
wishlist: [{
type: mongoose.Schema.Types.ObjectId,
ref: "Product",
}],
refreshToken: {
type: String,
},
}, {
timestamps: true,
});
userSchema.pre("save", async function (next) {
const salt = await bcrypt.genSaltSync(10);
this.password = await bcrypt.hash(this.password, salt);
});
userSchema.methods.isPasswordMatched = async function (enterdPassword) {
return await bcrypt.compare(enterdPassword, this.password);
};
//Export the model
module.exports = mongoose.model('User', userSchema);