-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
422 lines (369 loc) · 12.7 KB
/
server.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
var express = require('express');
var app = express();
var port = process.env.PORT || 8080;
var path = require('path');
//var cors = require('cors');
var bcrypt = require('bcrypt-nodejs');
// var hash = bcrypt.hashSync(configAuth.bcryptHash.Hash);
var configAuth = require('./config/auth');
//Passport
var passport = require('passport');
var flash = require('connect-flash');
var LocalStrategy = require('passport-local').Strategy;
//Session
var session = require('express-session');
app.use(session({
// secret: 'houseofgamessecret',
secret: configAuth.sessionSecret.secret,
saveUninitialized: true,
resave: true,
cookie: {
path: '/',
httpOnly: false,
maxAge: 1800000 //30 minute Timeout
}
}));
app.use(flash());
app.use(passport.initialize());
app.use(passport.session());
app.use(function(req, res, next){
if(req.user){
// console.log("USERNAME JSON: " + JSON.stringify(req.user));
res.locals.username = req.user.username;
res.locals.isLoggedIn = true;
} else {
res.locals.username = "None";
res.locals.isLoggedIn = false;
}
next();
});
app.use
//================Database Setup====================//
var pg = require('pg');
const Pool = require('pg-pool');
const url = require('url')
const connectionString = "postgres://nldtjebakdcwid:fd27e6eeeaa90bc9f691d2692d4d1c5632005215ba880f58fa5d0944bdbc428f@ec2-23-21-220-188.compute-1.amazonaws.com:5432/df6jlcpaevc1fe";
const params = url.parse(process.env.DATABASE_URL || connectionString);
const auth = params.auth.split(':');
const config = {
user: auth[0],
password: auth[1],
host: params.hostname,
port: params.port,
database: params.pathname.split('/')[1],
ssl: true
};
const pool = new Pool(config);
//====================Middleware========================//
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
var methodOverride = require('method-override');
app.set('views', __dirname);
app.set('view engine', 'ejs');
app.use('/scripts', express.static(__dirname + '/node_modules/bootstrap/dist/'));
// app.use('/css', express.static(__dirname + '/public/css'));
app.use('/public', express.static(__dirname + '/public'));
//=============Passport Session Setup=================//
passport.serializeUser(function(user, done) {
console.log("USER: " + user)
done(null, user);
});
passport.deserializeUser(function(user, done) {
done(null, user);
});
//Passport Login
passport.use('local-login', new LocalStrategy({
usernameField: 'username',
passwordField: 'password',
passReqToCallback : true
}, function(req, username, password, done){
console.log("Entering login strategy");
pool.query("SELECT * FROM users WHERE username = $1", [username], function(err, result){
if(err){
return done(err);
}
//Check if username exists
if(!result.rows.length){
console.log("Wrong username");
return done(null, false, req.flash('loginMessage', 'Incorrect username/password'));
}
//Check if password is the same
if(!bcrypt.compareSync(password, result.rows[0].password)){
console.log("Wrong password");
return done(null, false, req.flash('loginMessage', 'Incorrect username/password'));
}
else{
//Successful login
return done(null, result.rows[0]);
}
});
}));
//Passport Register/Signup
passport.use('local-signup', new LocalStrategy({
usernameField : 'username',
passwordField : 'password',
passReqToCallback : true // allows us to pass back the entire request to the callback
}, function(req, username, password, done){
process.nextTick(function(){
pool.query("SELECT * FROM users where username = $1", [req.body.username], function(err, result){
// console.log("Querying now...");
if(err){
//client.release();
return done(err);
}else{
if(result.rows[0] != undefined){
// already exists
console.log("Already exists");
return done(null, false, req.flash('signupMessage', 'That username is already taken. Please enter a new one'));
} else {
var user = {
username: username,
// password: password
password: bcrypt.hashSync(password, null, null)//hash password
};
var values = [req.body.fname, req.body.lname, user.username, req.body.email, req.body.gender, req.body.age, req.body.ethnicity, user.password];
pool.query("INSERT INTO users (fname, lname, username, email, gender, age, ethnicity, password) VALUES ($1, $2, $3, $4, $5, $6, $7, $8);", values, function(err, result){
//client.release();
if(err){
return done(err);
}else{
return done(null, user);
}
});
}
}
});
});
}));
//===================ROUTES===========================//
app.get('/', function(req, res) {
res.status(200);
res.render('pages/home',{
message: req.flash('unauthorized')
});
});
//Render Register page
app.get('/register', function(req, res){
res.status(200);
res.render('pages/register', { message: req.flash('signupMessage') });
});
//Go through passports local signup strategy
app.post('/register/auth', passport.authenticate('local-signup', {
successRedirect : '/pregame', // redirect to pregame questionnaire if success
failureRedirect : '/register', // redirect back to the signup page if there is an error
failureFlash : true // allow flash messages
}));
// Render Terms and Conditions Page
app.get('/disclaimer',function(req, res){
res.status(200);
res.render('pages/disclaimer');
});
app.get('/pregame', isLoggedIn, function(req, res){
res.status(200);
res.render('pages/pregame', {
message: req.flash('loginMessage')
});
});
app.post('/pregame/auth', function(req, res){
var values = [req.user.username, req.body.privacy_val, req.body.social_media, req.body.freq, req.body.fb_val];
pool.query("INSERT INTO pregame (username, priv_val, social_media, freq, tnc) VALUES ($1, $2, $3, $4, $5);", values, function(err, result){
if(err){
res.status(500).send('Internal Server Error when adding row into pregame table');
return;
}
});
res.status(200);
res.send({redirect: '/gameinfo'});
});
app.get('/gameinfo', isLoggedIn, function(req, res){
res.status(200);
res.render('pages/gameinfo');
});
app.put('/gameinfo/auth', function(req, res){
var values = [req.body.opponent, req.user.username];
pool.query("UPDATE users SET opponent=$1 WHERE username=$2;", values, function(err, result){
if(err){
res.status(500).send('Internal Server Error when updating row into users table');
return;
}
});
res.status(200);
});
app.get('/game', isLoggedIn, function(req, res){
// res.render('pages/game');
var result = {};
var value = [req.user.username];
pool.query('SELECT opponent FROM users WHERE username=$1', value, function(err, rows, fields){
if(err){
res.status(500).send('Internal Server Error when finding opponent');
return;
}
result.results = {opponent:rows.rows[0].opponent};
res.status(200);
res.render('pages/game', {result:result});
});
});
app.post('/game/auth', function(req, res){
var values = [req.user.username, req.body.round1money, req.body.round1status];
pool.query("INSERT INTO game1 (username, round1money, round1status) VALUES ($1, $2, $3);", values, function(err, result){
if(err){
res.status(500).send('Internal Server Error when adding row into game1 table');
return;
}
});
res.status(200);
});
app.get('/round2', isLoggedIn, function(req, res){
// res.render('pages/round2');
var result = {};
var value = [req.user.username];
pool.query('SELECT opponent FROM users WHERE username=$1', value, function(err, rows, fields){
if(err){
next(err);
res.status(500).send('Internal Server Error when finding opponent');
return;
}
result.results = {opponent:rows.rows[0].opponent};
res.status(200);
res.render('pages/round2', {result:result});
});
});
app.post('/round2/auth', function(req, res){
var values = [req.user.username, req.body.round2money, req.body.round2status];
pool.query("INSERT INTO game2 (username, round2money, round2status) VALUES ($1, $2, $3);", values, function(err, result){
if(err){
res.status(500).send('Internal Server Error when adding row into game2 table');
return;
}
});
res.status(200);
});
app.get('/round3', isLoggedIn, function(req, res){
// res.render('pages/round3');
var result = {};
var value = [req.user.username];
pool.query('SELECT opponent FROM users WHERE username=$1', value, function(err, rows, fields){
if(err){
res.status(500).send('Internal Server Error when finding opponent');
next(err);
return;
}
result.results = {opponent:rows.rows[0].opponent};
res.status(200);
res.render('pages/round3', {result:result});
});
});
app.post('/round3/auth', function(req, res){
var values = [req.user.username, req.body.round3money, req.body.round3status];
pool.query("INSERT INTO game3 (username, round3money, round3status) VALUES ($1, $2, $3);", values, function(err, result){
if(err){
res.status(500).send('Internal Server Error when adding row into game3 table');
return;
}
});
res.status(200);
});
app.get('/summary', isLoggedIn, function(req, res){
// Have query to get info from round1 table to send over to front end to show round 1 results
var result = {};
var value = [req.user.username];
pool.query('SELECT round1status, round1money FROM game1 WHERE username=$1', value, function(err, rows, fields){
if(err){
res.status(500).send('Internal Server Error when showing summary page');
next(err);
return;
}
result.results = {status:rows.rows[0].round1status,
money: rows.rows[0].round1money};
res.status(200);
res.render('pages/summary', {result:result});
});
});
app.get('/summary2', isLoggedIn, function(req, res){
// Have query to get info from round1 table to send over to front end to show round 2 results
var result = {};
var value = [req.user.username];
pool.query('SELECT round2status, round2money FROM game2 WHERE username=$1', value, function(err, rows, fields){
if(err){
res.status(500).send('Internal Server Error when showing summary page');
next(err);
return;
}
result.results = {status:rows.rows[0].round2status,
money: rows.rows[0].round2money};
res.status(200);
res.render('pages/summary2', {result:result});
});
});
app.get('/summary3', isLoggedIn, function(req, res){
// Have query to get info from round1 table to send over to front end to show overall results
var result = {};
var value = [req.user.username];
pool.query('SELECT * FROM users NATURAL JOIN game1 NATURAL JOIN game2 NATURAL JOIN game3 WHERE username=$1', value, function(err, rows, fields){
if(err){
res.status(500).send('Internal Server Error when showing summary page');
next(err);
return;
}
result.results = {
opponent: rows.rows[0].opponent,
status1: rows.rows[0].round1status,
status2: rows.rows[0].round2status,
status3:rows.rows[0].round3status,
money1: rows.rows[0].round1money,
money2: rows.rows[0].round2money,
money3: rows.rows[0].round3money
};
res.status(200);
res.render('pages/summary3', {result:result});
});
});
app.get('/postgame', isLoggedIn, function(req, res){
res.status(200);
res.render('pages/postgame', {
message: req.flash('loginMessage')
});
});
app.post('/postgame/auth', function(req, res){
var values = [req.user.username, req.body.lotto, req.body.urn, req.body.real_person, req.body.purpose, req.body.concern];
pool.query("INSERT INTO postgame (username, lotto, urn, real_person, purpose, concern) VALUES ($1, $2, $3, $4, $5, $6);", values, function(err, result){
if(err){
res.status(500).send('Internal Server Error when adding row into table');
return;
}
});
res.status(200);
res.send({redirect: '/finish'});
});
app.get('/finish', function(req, res){
res.status(200);
res.render('pages/finish');
});
app.get('/login', function(req, res){
res.status(200);
res.render('pages/login', { message: req.flash('loginMessage') });
});
app.post('/login/auth', passport.authenticate('local-login', {
successRedirect : '/pregame', // redirect to pregame questionnaire if success
failureRedirect : '/login', // redirect back to the signup page if there is an error
failureFlash : true // allow flash messages
}));
app.get('/logout', function(req, res){
req.logout();
res.redirect('/');
});
app.listen(port, function(){
console.log("Listening on port " + port);
});
function isLoggedIn(req, res, next) {
// if user is authenticated in the session, carry on
if (req.isAuthenticated()){
return next();
}
// if they aren't redirect them to the home page
req.flash('unauthorized', 'You do not have permission to access this content. Please login or sign up first!');
res.redirect('/');
}