forked from rawat-aaryan/test
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.js
More file actions
35 lines (32 loc) · 884 Bytes
/
schema.js
File metadata and controls
35 lines (32 loc) · 884 Bytes
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
const mongoose = require("mongoose");
const bcrypt = require("bcryptjs");
const jwt=require("jsonwebtoken");
var userSchema = new mongoose.Schema({
email: String,
password: String,
profile: String,
tokens:[
{
token: String,
}
]
});
userSchema.pre("save", async function (next) {
if (this.isModified("password")) {
this.password = await bcrypt.hash(this.password, 12);
console.log(this.password);
}
next();
});
userSchema.methods.generateAuthToken =async function(){
try{
let token=jwt.sign({_id:this._id},"ABCDE");
this.tokens=this.tokens.concat({token:token})
await this.save();
return token;
}catch(e){
console.log(e);
}
}
var User = mongoose.model("User", userSchema);
module.exports = User;