-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
134 lines (123 loc) · 3.67 KB
/
index.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
const express = require('express');
const app = express();
const fetch = require("node-fetch");
const path = require('path');
require('dotenv').config();
const bcrypt = require('bcryptjs');
const signup=require('./database/conn.js');
const mongoose= require('mongoose');
const session=require('express-session');
const MongoDbSession=require('connect-mongodb-session')(session);
const storesession= new MongoDbSession({
uri:"mongodb+srv://Himanshu_Lulla:[email protected]@cluster0.fg2gf.mongodb.net/himanshu?retryWrites=true&w=majority",
collection:'mysessions'
});
app.use(session({
secret:process.env.SECRET_KEY,
resave:false,
saveUninitialized:false,
store:storesession
}));
const port= process.env.PORT || 8000;
app.set('view engine','hbs');
app.use(express.json());
app.use(express.urlencoded({extended:false}));
const staticpath=path.resolve('./public');
app.use(express.static(staticpath));
const authentication=(req,res,next)=>{
if(!req.session.isAuth){
res.redirect('/');
}
else{
next();
}
}
app.get('/',(req,res)=>{
res.render('login1',{
flag:false
});
});
app.get('/signup',(req,res)=>{
res.render('signup1');
});
app.post('/signup',async(req,res)=>{
console.log(req.body);
let {Username,Email,Phone,Password,RePassword}=req.body;
try{
Password= await bcrypt.hash(Password,10);
RePassword= await bcrypt.hash(RePassword,10);
var document = new signup({
Username:Username,
Email:Email,
Phoneno:Phone,
Password:Password,
Repassword:RePassword
});
await document.save();
res.redirect('/');
}catch(err){
console.log(err.message);
res.statusCode=401;
res.json({'error':'User with email id already exists'});
}
});
app.post('/',async(req,res)=>{
let {username,password} = req.body;
try{
const data= await signup.findOne({Email:username});
if(!data){
res.render('signup1');
}
else{
const pass = data.Password;
const status = await bcrypt.compare(password,pass);
if(!status){
res.status(401);
res.render('login1',{
flag:true
});
}
else{
req.session.isAuth=true;
res.redirect('quizhomepage');
}
}
}
catch(err){
console.log(err);
}
});
app.get('/quizhomepage',authentication,(req,res)=>{
res.render('quizhomepage');
});
app.get('/quiz',authentication,(req,res)=>{
const category = req.query.category;
const fetchdata = async (category)=>{
try{
const data = await fetch(`https://quizapi.io/api/v1/questions?apiKey=dVExB1tQVBUtzyBG3fphSImbItC6e8aBuPWr7lkO&category=${category}&limit=1`);
const actual = await data.json();
const ans =Object.values(actual[0].correct_answers);
const answers=["answer_a","answer_b","answer_c","answer_d","answer_e","answer_f"];
res.render('quizpage1',{
quizdata:actual[0],
category:category,
correctans:answers[ans.indexOf("true")]
});
}catch(err){
console.log(err);
}
}
fetchdata(category);
});
app.get('/logout',(req,res)=>{
req.session.destroy((err)=>{
if(err) throw err;
res.redirect('/');
});
});
app.get('*',(req,res)=>{
res.render('error');
});
app.listen(port,()=>{
console.log(`Server running at port ${port}`);
});