-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuserModel.js
36 lines (36 loc) · 1.03 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
const mongoose =require("mongoose");
const bcrypt = require('bcryptjs');
const userModel = mongoose.Schema(
{
name:{type:String,trim:true,required:true},
email:{type:String,trim:true,required:true,unique:true},
password:{type:String,required:true},
pic:{type:String,
required:true,
default: "https://icon-library.com/images/anonymous-avatar-icon/anonymous-avatar-icon-25.jpg"
},
isAdmin: {
type: Boolean,
required:true,
default:false,
},
},
{
timestamps:true,
}
);
//
userModel.methods.matchPassword = async function (enterPass){
return bcrypt.compare(enterPass,this.password);
}
//before save run given function
userModel.pre("save",async function (next){
if(!this.isModified){
next();
}
//before sending it to database it encript our password
const salt= await bcrypt.genSalt(10);
this.password = await bcrypt.hash(this.password,salt);
})
const User = mongoose.model("User",userModel);
module.exports=User;