-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
207 lines (156 loc) · 4.82 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
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
if(process.env.NODE_ENV != "production"){
require("dotenv").config({path: __dirname + '/.env'});
}
const express = require("express");
const app = express();
const path = require("path");
const mongoose = require("mongoose");
const User = require("./models/user");
const bodyParser = require("body-parser");
const bcrypt = require("bcrypt");
const session = require("express-session");
const MongoDBStore = require("connect-mongo")(session);
const flash = require("connect-flash");
var urlObj;
const dbUrl = process.env.DB_URL || "mongodb://localhost:27017/WebD_Project";
const db = process.env.DB
//const dbUrl = "mongodb://localhost:27017/WebD_Project";
mongoose.connect(dbUrl, {useNewUrlParser: true, useUnifiedTopology: true, dbName: db})
.then(() => {
console.log("DB CONNECTED!");
})
.catch(err => {
console.log("OOPS! Error ocurred in connecting to mongo!");
console.log(err);
});
const secret = process.env.SECRET;
const store = new MongoDBStore({
url: dbUrl,
secret,
touchAfter: 24*60*60
})
store.on("error", (e) => {
console.log("SESSION STORE ERROR: ", e);
})
app.use(express.static(path.join(__dirname, '/')));
app.use("/public", express.static('./public/'));
app.use(express.static(path.join(__dirname,'public')));
app.use(bodyParser.urlencoded({extended : true}));
app.use(express.json({limit : "1mb"}));
app.use(session({store, secret, resave: false, saveUninitialized: true}));
app.use(flash());
app.set('view engine', 'ejs')
app.set('views', path.join(__dirname, '/views'))
const requireLogin = (req, res, next) => {
if(!req.session.user_id){
return res.redirect("/login");
}
next();
}
app.get("/", (req,res) => {
res.render("home");
})
app.get("/register", (req,res) => {
res.render("register");
})
app.post("/register", async (req, res) => {
const { username, password, email } = req.body;
const hash = await bcrypt.hash(password, 12);
const user = new User({
username, password: hash, email, totalCorrect: 0, totalIncorrect: 0, totalSkipped: 0,
})
await user.save();
req.session.user_id = user._id;
req.flash("success", "Hey "+username+"! You're successfully logged in.");
res.redirect("/explore");
})
app.get("/login", (req,res) => {
res.render("login", {messages : req.flash("failure")});
})
app.post("/login",async (req,res) => {
const { username, password } = req.body;
const user = await User.findOne({username});
const validPassword = await bcrypt.compare(password, user.password);
if(validPassword){
//console.log("You are succesfully logged in.");
req.session.user_id = user._id;
req.flash("success", "Hey "+username+"! You're successfully logged in.");
res.redirect("/explore");
}
else{
//console.log("Incorrect username or password!");
req.flash("failure", "Incorrect username or password!");
res.redirect("/login");
}
})
app.get("/logout", (req, res) => {
req.session.user_id = null;
res.redirect("/login");
})
app.get("/explore", requireLogin, (req, res) => {
res.render("explore", {messages : req.flash("success")});
})
app.post("/explore", requireLogin, (req, res) => {
urlObj = req.body;
//console.log(urlObj);
res.json({
status: "success"
});
})
app.get("/practice", requireLogin, (req, res) => {
res.cookie("url", urlObj.url);
res.render("practice");
})
app.get("/dashboard", requireLogin, async (req, res) => {
const user_id = req.session.user_id;
const user = await User.findById(user_id);
const username = user.username;
const Qoins = user.totalCorrect;
const correct = Qoins;
const incorrect = user.totalIncorrect;
const skipped = user.totalSkipped;
const sum = correct + incorrect + skipped;
const pCorrect = Math.round((correct*100)/sum);
const pIncorrect = Math.round((incorrect*100)/sum);
const pSkipped = Math.round((skipped*100)/sum);
const accuracy = Math.round((correct/(correct+incorrect))*100);
res.render("dashboard", {
username : username,
Qoins : Qoins,
correct : correct,
incorrect : incorrect,
skipped : skipped,
pCorrect : pCorrect,
pIncorrect : pIncorrect,
pSkipped : pSkipped,
accuracy : accuracy
});
})
app.get("/leaderboard", requireLogin, async (req, res) => {
const user_id = req.session.user_id;
const user = await User.findById(user_id);
const username = user.username;
const Qoins = user.totalCorrect;
const rankings = await User.find().sort({totalCorrect: -1});
res.render("leaderboard", {
rankings: rankings,
username: username,
Qoins: Qoins
});
})
app.post("/updateDb", requireLogin, async (req, res) => {
const data = req.body;
const user_id = req.session.user_id;
const user = await User.findById(user_id);
user.totalCorrect += (data.correct);
user.totalIncorrect += (data.incorrect);
user.totalSkipped += (data.skipped);
await user.save();
res.json({
status: "success"
});
})
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`Serving on port ${port}`);
})