-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStudSchema.js
61 lines (50 loc) · 1.17 KB
/
StudSchema.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
const express= require("express");
const mongoose =require("mongoose");
const bcrypt = require("bcrypt");
const jwt= require("jsonwebtoken");
const StudSchema= new mongoose.Schema({
name:{
type:String,
require:true
},
email:{
type:String,
require:true
},
password:{
type:String,
require:true,
},
confirmPassword:{
type:String,
require:true,
},
tokens:
[{token:{
type:String,
require:true
}}]
})
// generating tokens
StudSchema.methods.generateAuthToken = async function(){
try{
const token = jwt.sign({_id:this._id.toString()}, process.env.SECRET_KEY);
this.tokens = this.tokens.concat({token:token})
await this.save();
return token
}
catch(e){
console.log("error occured while generating a token"+ e)
}
}
StudSchema.pre(`save`, async function(next){
console.log("hi from here")
if(this.isModified(`password`)){
this.password = await bcrypt.hash(this.password, 10);
console.log(`password is ${this.password}`)
this.confirmPassword = undefined;
}
next()
})
const Stud = new mongoose.model("Stud", StudSchema);
module.exports = Stud;