-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
143 lines (114 loc) · 3.57 KB
/
app.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
require('dotenv').config();
const express = require("express");
const app = express();
const path =require("path")
const hbs = require("hbs");
const auth =require("./auth.js")
const cookieParser = require("cookie-parser");
app.use(cookieParser());
const {json}= require("express");
const bcrypt = require("bcrypt");
app.use(express.urlencoded({extended:false}));
require("./conn.js")
const static_path = path.join(__dirname, "./public");
const template_path = path.join(__dirname, "./templates/views");
const partials_path = path.join(__dirname, "./templates/partials")
const Stud = require("./StudSchema.js");
app.set("view engine", "hbs")
app.set("views",template_path )
hbs.registerPartials(partials_path)
app.use(express.static(static_path));
app.listen(8000,(req,res)=>{
console.log("listening at port 8000")
})
app.get("/logout", auth, async(req,res)=>{
res.clearCookie("jwt");
res.send("sucessfully logout")
// req.user.tokens = req.user.tokens.filter((token)=>{
// return token.token !== req.token
// })
// await req.user.save()
req.user.tokens = []
console.log(req.user)
})
app.get("/about", auth,(req,res)=>{
res.send("ok verified")
})
app.use(express.json());
app.get("/",(req,res)=>{
res.render("index");
})
app.get("/login",(req,res)=>{
res.render("login");
})
console.log(process.env.SECRET_KEY)
app.post("/user", async(req,res)=>{
try{
const e=req.body.email2
const user3 = await Stud.find({email:e});
const password2= req.body.password2
console.log(user3)
console.log(user3[0].password)
const result2= await bcrypt.compare(password2, user3[0].password)
// const email = req.body.email2
// const password = req.body.password2
// res.send(`the entered email is ${email} and password is ${password}`)
const token= await user3[0].generateAuthToken()
console.log("the token is "+ token);
res.cookie("jwt", token, {
expires: new Date(Date.now()+30000),
httpOnly:true
});
if(result2){
res.send("welcome user")
}
else{
res.send("nahi bhai details match nahi ki ")
}
}catch(e){
res.send("invalid details")
console.log(e)
}
})
app.post("/register",async(req,res)=>{
try{
const password = req.body.password
const cpassword = req.body.confirmPassword
console.log(req.body.name)
console.log(cpassword)
if(password === cpassword){
const doc = new Stud({
name:req.body.name,
email:req.body.email,
password: password,
confirmPassword:cpassword
});
console.log("perfect")
// const token= await doc.generateAuthToken();
// console.log("the token is "+ token);
const result = await doc.save();
res.send("registration completed");}
else if(password!== cpassword){
res.send("password doesn't match")
}
else{
res.send("plz fill form correctly")
}
}catch(e){
console.log(e)
res.send("kindly fill the form properly")
}
})
const fnd = async()=>{
try{
const user2 = await Stud.find({email:"bha@123"})
const result = await bcrypt.compare("b",user2[0].password)
console.log(user2)
console.log(result)
console.log(user2[0].password)
}
catch(e){
console.log(e)
}
}
// fnd();