-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
340 lines (282 loc) · 8.22 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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
require("dotenv").config();
const express = require("express");
const bodyParser = require("body-parser");
const ejs = require("ejs");
const mongoose = require("mongoose");
const session = require("express-session");
const passport = require("passport");
const passportLocalMongoose = require("passport-local-mongoose");
const GoogleStrategy = require("passport-google-oauth20").Strategy;
const findOrCreate = require("mongoose-findorcreate");
const cookieSession = require("cookie-session")
/* const md5 = require("md5"); */
/* const bcrypt = require("bcrypt");
const saltRounds = 10; */
const app = express();
app.use(cookieSession({
secret: process.env.SECRET,
maxAge:24*60*60*60
}));
app.use(passport.initialize());
app.use(passport.session());
mongoose.connect(process.env.MONGODB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false
});
mongoose.set("useCreateIndex", true);
app.use(express.static("public"));
app.set("view engine", "ejs");
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(express.json());
// Model for General Quiz Data
const Data = require('./models/dataModel.js')
//Model to save Question
const Question = require('./models/saveQuestion')
// First and Third Type + OIR TESTS
const {First,Third,OirTestOne,OirTestTwo,OirTestThree,OirTestFour,OirTestFive} = require('./models/quizModel')
// we need to change schema to encrypt
const userSchema = new mongoose.Schema({
email: String,
username: String,
googleId: String,
picture: String
});
userSchema.plugin(passportLocalMongoose);
userSchema.plugin(findOrCreate);
/* console.log(process.env.SECRET); */
/* userSchema.plugin(encrypt, {secret: process.env.SECRET, encryptedFields: ["password"]}); */
// it must be declared above model
const User = mongoose.model("User", userSchema);
passport.use(User.createStrategy());
passport.serializeUser(function (user, done) {
done(null, user.id);
});
passport.deserializeUser(function (id, done) {
User.findById(id, function (err, user) {
done(err, user);
});
});
let myEmail = "";
passport.use(new GoogleStrategy({
clientID: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
callbackURL: "https://fsb-quiz.herokuapp.com/auth/google/home",
userProfileURL: "https://www.googleapis.com/oauth2/v3/userinfo"
},
function (accessToken, refreshToken, profile, cb) {
console.log(profile);
User.findOrCreate({
googleId: profile.id,
username: profile.displayName,
email: profile.emails[0].value,
picture: profile.photos[0].value
}, function (err, user) {
console.log(user);
/* myEmail = user.email; */
return cb(err, user);
});
}
));
app.get('/auth/google',
passport.authenticate('google', {
scope: ['profile', "email"]
})
);
app.get('/auth/google/home',
passport.authenticate('google', {
failureRedirect: '/login'
}),
function (req, res) {
// Successful authentication, redirect home.
res.redirect('/landing');
});
app.get("/", (req, res) => {
res.render("index");
})
app.get("/landing", function (req, res) {
let firstTime = 0;
let count = 0;
let firstHighScore = 0;
let secondHighScore = 0;
let thirdHighScore = 0;
let firstCount = 0;
let secondCount = 0;
let thirdCount = 0;
let picture = "";
if (req.isAuthenticated()) {
console.log(req.user);
picture = req.user.picture;
Data.find({
email: req.user.email
}, function (err, data) {
if (err) {
console.log(err);
} else {
console.log(data);
console.log("length", data.length);
/* checking whether it is first time user or returning user */
if (data.length == 0) {
firstTime = 0;
console.log("first time User");
res.render("landing", {
email: req.user.email,
username: req.user.username,
picture: picture,
typeOfUser: firstTime,
count: count,
firstHighScore: firstHighScore,
firstCount: firstCount,
secondHighScore: secondHighScore,
secondCount: secondCount,
thirdHighScore: thirdHighScore,
thirdCount: thirdCount
});
} else {
console.log("returning User");
console.log(data);
count = 1;
firstTime = 1;
/* Collectng Data from First Quiz */
res.render("landing", {
email: req.user.email,
username: req.user.username,
picture: picture,
typeOfUser: firstTime,
count: count,
firstHighScore: data[0].firstHighScore,
firstCount: data[0].firstCount,
secondHighScore: data[0].secondHighScore,
secondCount: data[0].secondCount,
thirdHighScore: data[0].thirdHighScore,
thirdCount: data[0].thirdCount
});
}
}
})
} else {
res.redirect("/login");
}
});
// ssb-admin route
app.get("/ssb-admin",async(req,res)=>{
if(req.user.email===process.env.ADMIN_ID1 || req.user.email===process.env.ADMIN_ID2)
{
const result = await Second.find({ status: "False" })
console.log("res"+result)
await res.render("ssb-admin/ssb-admin",{user:req.user,response: result});
}
else{
res.redirect("/landing");
}
})
app.get("/quizsetup", (req, res) => {
res.render("quizsetup");
})
app.get("/questionlogic", (req, res) => {
let question = "";
Question.find({}, function (err, ques){
if (err) {
console.log(err);
}
else {
console.log(ques);
res.render("questionlogic",{ question: ques});
}
})
})
// OIRTEST SECTION
const oirTestRoute = require('./routes/oirtest')
app.use('/oir',oirTestRoute)
// PPDTTEST SECTION
const ppdtTestRoute = require('./routes/ppdt');
const Second = require("./models/ppdtModel.js");
app.use('/ppdt',ppdtTestRoute)
// Get Question for OIR TEST
app.get("/findQuestion/:token", (req, res) => {
let question = "";
console.log("get request made");
console.log(req.params.token);
Question.find({}, function (err, ques){
if (err) {
console.log(err);
}
else {
console.log(ques[req.params.token]);
res.send(ques[req.params.token]);
}
})
})
// Save question page
app.get("/quizsetup",(req,res)=>{
res.render("quizsetup");
})
// Post Request Handling for Saving questions to Database
app.post("/savequestion", (req, res) => {
const ques = new Question(req.body);
ques.save();
res.send("Successfully Saved");
})
app.get("/logout", function (req, res) {
req.logOut();
res.redirect("/");
});
app.get("/register", function (req, res) {
res.render("register");
})
app.get("/login", function (req, res) {
res.render("login");
})
// Profile Page Get Request
app.get("/profile", (req, res) => {
let picture = "";
if (req.isAuthenticated()) {
picture = req.user.picture;
res.render("profile", {
username: req.user.username,
email: req.user.email,
picture: picture
});
}
else {
res.redirect("/login");
}
})
// Landing Page Line Chart and Pie Chart Data Collection
/*app.get("/firstQuiz/:token", (req, res) => {
console.log("Token Info");
console.log(req.params.token);
First.find({ email: req.params.token }, function (err, one) {
if(err){
console.log(err);
}
else {
res.send(one);
}
})
})
*/
// Save Data to General Model
app.post("/saveData", (req,res) => {
if (req.isAuthenticated()) {
console.log("post request made");
const firstHighScore = req.body.firstHighScore;
const firstCount = req.body.firstCount;
console.log("first Count");
console.log(firstCount);
const entry = new Data({
email: req.user.email,
firstHighScore: firstHighScore,
firstCount: firstCount,
});
entry.save();
}
else {
res.redirect("/login");
}
})
app.listen(process.env.PORT || 3000, function () {
console.log("server is running on port 3000");
});