-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
178 lines (137 loc) · 4.77 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//Required packages
require('dotenv').config()
const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const cookieParser = require("cookie-parser");
const cors = require("cors");
const app = express();
var jwt = require('jsonwebtoken');
const path = require('path');
//const auth=require("./sample");
const authRoutes = require("./routes/auth");
const leader = require("./routes/leaderboard");
//Middle wares
app.use(express.json());
app.use(express.urlencoded({ extended: true }))
app.use(bodyParser.json());
app.use(express.static(__dirname + '/Questions'));
app.use(express.static(__dirname+ '/landingpage'));
app.use(express.static(__dirname+ '/Questions/images'));
app.use(express.static(__dirname+ '/Questions/css'));
app.use(cors());
//cookies
app.use(cookieParser('null_chapter_is_the_best'));
//app.use(auth);
//db connection
const db = async()=> {mongoose.connect(process.env.DATABASE2,
{useNewUrlParser: true,
useUnifiedTopology:true,
useCreateIndex:true
}).then(()=>{
console.log("DB CONNECTED")
})
};
db();
//My Routes
app.use("/api", authRoutes);
app.use("/api/leader",leader);
//PORT
const PORT = process.env.PORT || 8080;
//Starting a server
app.listen(PORT,()=>{
console.log(`app is running at ${PORT}`);
})
const maxAge = 3*24*60*60;
const createToken = (id,answered) => {
return jwt.sign({ id, answered}, process.env.SECRET1, {
expiresIn: maxAge
});
};
app.get('/',function(req, res){
if(req.cookies['timeup']==undefined){
if(req.cookies['loggedin']=="false" && req.cookies['attempt']!=undefined){
return res.sendFile(path.join(__dirname , '/landingpage/sam.html'));
//return res.sendFile(path.join(__dirname , '/webhunt3.0.html'));
}
if(req.cookies['attempt']==undefined){
res.cookie('attempt',0,{maxAge:maxAge*1000});
return res.sendFile(path.join(__dirname , '/landingpage/sam.html'));
//return res.sendFile(path.join(__dirname , '/webhunt3.0.html'));
}
else if(req.cookies['loggedin']=="true"){
console.log("logged");
if(req.cookies['attempt']==20){
return res.sendFile(path.join(__dirname,'/Questions/html/congrats.html'));
}
var attemptChecking= Number(req.cookies['attempt'])+1;
console.log(attemptChecking);
return res.redirect('/question/'+attemptChecking);
}
}
else{
return res.sendFile(path.join(__dirname+'/errors/nolonger.html'));
}
})
app.get('/signin', function(req, res) {
if(req.cookies['timeup']==undefined){
if(req.cookies['loggedin']=="true"){
if(req.cookies['attempt']==20){
return res.sendFile(path.join(__dirname,'/Questions/html/congrats.html'));
}
var attemptChecking= Number(req.cookies['attempt'])+1;
return res.redirect('/question/'+attemptChecking);
}
else{
return res.sendFile(path.join(__dirname , '/landingpage/signin.html'));
//return res.sendFile(__dirname + '/welcome.html');
}
}
else{
return res.sendFile(path.join(__dirname , '/errors/nolonger.html'));
}
});
app.get('/congratulations',(req, res)=>{
res.sendFile(path.join(__dirname+"/Questions/html/congrats.html"));
});
app.get('/question/:id',(req,res)=>{
const token=req.cookies.jwt;
//for 1st question
if(req.cookies['loggedin']=="true"){
if(req.params.id==1){
const token1= createToken(req.params.id,0);
res.cookie('jwt',token1,{maxAge:maxAge*1000});
return res.sendFile(__dirname+'/Questions/html/challenge1.html');
//return res.sendFile(__dirname+'/Questions/html/question1.html');
}
//for 2nd question
else if(req.params.id!=1){
var newid = Number(req.params.id);
// console.log("id"+newid);
if (token) {
// console.log(token);
jwt.verify(token, process.env.SECRET1, (err, decodedToken) => {
if (err) {
console.log(err.message);
res.redirect('/signin');
} else if(((newid)>=decodedToken.id) && (decodedToken.answered==newid-1)){
//console.log(decodedToken);
const token1= createToken(newid,newid-1);
res.cookie('jwt',token1,{maxAge:maxAge*1000});
//return res.sendFile(__dirname,'/Questions/html/challenge1.html');
return res.sendFile(__dirname+'/Questions/html/challenge'+ newid+'.html');
}
else{
return res.sendFile(path.join(__dirname,'/errors/notauth.html'));
}
});
}
else {
res.redirect('/signin');
}
}
} else{
return res.sendFile(__dirname+'/errors/login.html');
}
});
module.exports = app;