forked from Prateeknandle/Ask_IIITM-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserSchema.js
More file actions
50 lines (43 loc) · 1.01 KB
/
UserSchema.js
File metadata and controls
50 lines (43 loc) · 1.01 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
const jwt = require('jsonwebtoken');
const mongoose = require('mongoose');
const bcrypt = require('bcryptjs');
const UserSchema = new mongoose.Schema({
name:{
type:String,
required:true
},
email:{
type:String,
required:true
},
password:{
type:String,
required:true
},
tokens:[
{
token:{
type: String,
required: true
}
}
]
})
UserSchema.pre('save',async function(next){
if(this.isModified('password')){
this.password = await bcrypt.hash(this.password, 12);
}
next();
});
UserSchema.methods.genrateAuthToken = async function (){
try{
let token = jwt.sign({_id:this._id},process.env.SECRET_KEY);
this.tokens = this.tokens.concat({token:token});
await this.save();
return token;
}catch(err){
console.log(err);
}
}
const User = module.exports = mongoose.model('USER', UserSchema);
module.exports = User;