-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
520 lines (456 loc) · 15.1 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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
//#region ********* HEADER********************
// * Name: Supriya Mutharasan
// * Course: WEB322
// * Professor: Dr.Sharmin Ahmed
// * Date: 12/10/2020
//#endregion
//#region setup our modules
const HTTP_PORT = process.env.PORT || 8080;
const express = require('express');
const app = express();
const server = require('http').Server(app);
// const server = app.listen(HTTP_PORT);
const io = require('socket.io')(server);
const amazonScraper = require('amazon-buddy');
const kijiji = require('kijiji-scraper');
const path = require('path');
const XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
const _ = require('underscore');
const fs = require('fs');
const moment = require('moment');
const mongoose = require('mongoose');
const bcrypt = require('bcryptjs'),
SALT_WORK_FACTOR = 10;
const bodyParser = require('body-parser');
const exphbs = require('express-handlebars');
const clientSessions = require('client-sessions');
const { v4: uuidV4 } = require('uuid');
const UserModel = require('./static/src/UserModel');
const config = require('./static/src/config.js');
const connectionString = config.atlas_database_connection_string;
// some kind of user directory
//#endregion
//#region set middleware
app.use(express.static('static'));
app.use(express.static('node_modules'));
// Setup client-sessions
app.use(
clientSessions({
cookieName: 'session', // this is the object name that will be added to 'req'
secret: 'seneca_hackathon_cokato_social_media_app', // this should be a long un-guessable string.
duration: 200 * 60 * 1000, // duration of the session in milliseconds (2 minutes)
activeDuration: 1000 * 60, // the session will be extended by this many ms each request (1 minute)
})
);
var urlenParser = bodyParser.urlencoded({
extended: true,
});
// connect to your mongoDB database
mongoose.connect(connectionString, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
});
// log when the DB is connected
mongoose.connection.on('open', () => {
console.log('Database connection open.');
});
//#region // Server side validation
function serverSideSignUpFormValidate(givenUserObj) {
/*Minimum ten characters, at least one uppercase letter, one lowercase letter, one number and one special character: */
const passwordExp = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{6,}$/;
var validationFlag = true;
var signUpFormFirstName = givenUserObj.signUpFormFirstName;
var signUpFormLastName = givenUserObj.signUpFormLastName;
var signUpFormUsername = givenUserObj.signUpFormUsername;
var signUpFormEmail = givenUserObj.signUpFormEmail;
var signUpFormPassword = givenUserObj.signUpFormPassword;
if (signUpFormFirstName === '') validationFlag = false;
if (signUpFormLastName === '') validationFlag = false;
if (signUpFormUsername === '') validationFlag = false;
if (signUpFormEmail === '') validationFlag = false;
if (!signUpFormPassword.match(passwordExp)) validationFlag = false;
return validationFlag;
}
function serverSideSignInFormValidate(givenUserObj) {
const passwordExp = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{6,}$/;
var validationFlag = true;
var signInFormUsername = givenUserObj.signInFormUsername;
var signInFormPassword = givenUserObj.signInFormPassword;
if (signInFormUsername === '') validationFlag = false;
if (!signInFormPassword.match(passwordExp)) validationFlag = false;
return validationFlag;
}
function ensureLogin(req, res, next) {
if (!req.session.user) {
res.redirect('/home#modalLoginForm');
} else {
next();
}
}
function ensureAdminLogin(req, res, next) {
if (!req.session.user.isAdmin) {
res.redirect('/home#modalLoginForm');
} else {
next();
}
}
// call this function after the http server starts listening for requests
function onHttpStart() {
console.log('Express http server listening on: ' + HTTP_PORT);
}
//#endregion
//#endregion
//#region Register handlerbars as the rendering engine for views
app.set('views', './views');
app.set('layout', './views/layout');
app.engine(
'.hbs',
exphbs({
extname: '.hbs',
json: function (context) {
return JSON.stringify(context);
},
})
);
app.set('view engine', '.hbs');
//#endregion
//#region Setting up routes that use GET Method
app.get('/', (req, res) => {
res.redirect('/home');
});
app.get('/home', (req, res) => {
res.render('viewHome', {
registeredUser: req.session.user,
errorsPresent: req.session.err,
layout: 'home', // do not use the default Layout (main.hbs)
});
});
app.get('/contactUs', (req, res) => {
res.render('viewContactUs', {
registeredUser: req.session.user,
errorsPresent: req.session.err,
layout: 'homeBasic', // do not use the default Layout (main.hbs)
});
});
app.get('/about', (req, res) => {
res.render('viewAbout', {
registeredUser: req.session.user,
errorsPresent: req.session.err,
layout: 'home', // do not use the default Layout (main.hbs)
});
});
app.get('/shop', (req, res) => {
amazonScraper
.products({
keyword: 'medical supplies & equipment',
number: 1,
country: 'CA',
})
.then((prods) => {
let results = prods.result.slice(0, 10);
return results;
})
.then((givenProducts) => {
let validShopStatus = true;
if (givenProducts) {
res.render('viewShop', {
registeredUser: req.session.user,
errorsPresent: req.session.err,
retrievedProducts: givenProducts,
validShopStatus: validShopStatus,
layout: 'homeBasic', // do not use the default Layout (main.hbs)
});
} else {
validShopStatus = false;
res.render('viewShop', {
registeredUser: req.session.user,
errorsPresent: req.session.err,
retrievedProducts: givenProducts,
validShopStatus: validShopStatus,
layout: 'homeBasic', // do not use the default Layout (main.hbs)
});
}
})
.catch(console.error);
});
app.get('/news', (req, res) => {
const xhttp = new XMLHttpRequest();
//http://newsapi.org/v2/top-headlines?country=ca&category=health&apiKey=a53b6bfe3090434d97c0069edda96c77
xhttp.open(
'GET',
'http://newsapi.org/v2/top-headlines?country=ca&category=health&apiKey=a53b6bfe3090434d97c0069edda96c77',
false
);
xhttp.send();
const news = JSON.parse(xhttp.responseText);
res.render('viewNews', {
registeredUser: req.session.user,
errorsPresent: req.session.err,
givenNewsOfTheDay: news.articles.slice(0, 10),
layout: 'homeBasic', // do not use the default Layout (main.hbs)
});
});
app.get('/dashboard', ensureLogin, (req, res) => {
if (!req.session.user.isAdmin) {
UserModel.findOne({
user_name_user: req.session.user.username,
})
.lean()
.exec()
.then((result) => {
res.render('viewDashboard', {
registeredUser: req.session.user,
errorsPresent: req.session.err,
layout: 'homeBasic', // do not use the default Layout (main.hbs)
});
})
.catch((err) => {
console.error(err);
});
} else res.redirect('/adminDashboard');
});
app.get('/adminDashboard', ensureLogin, ensureAdminLogin, (req, res) => {
res.render('viewAdminDashboard', {
registeredUser: req.session.user,
errorsPresent: req.session.err,
layout: 'home', // do not use the default Layout (main.hbs)
});
});
app.get('/requestRoom', ensureLogin, (req, res) => {
res.redirect(`/room/${uuidV4()}`);
});
app.get('/room/:roomID', ensureLogin, (req, res) => {
const givenRoomID = req.params.roomID;
res.render('viewRoom', {
registeredUser: req.session.user,
registeredUserScript: JSON.stringify(req.session.user),
errorsPresent: req.session.err,
roomID: givenRoomID,
layout: 'homeBasic', // do not use the default Layout (main.hbs)
});
});
io.on('connection', (socket) => {
socket.on('join-room', (roomID, userID) => {
socket.join(roomID);
socket.to(roomID).broadcast.emit('user-connected', userID);
socket.on('disconnect', () => {
socket.to(roomID).broadcast.emit('user-disconnected', userID);
});
});
});
app.get('/logout', (req, res) => {
//registeredUser.validState = false;
req.session.reset();
res.redirect('/home');
});
//#endregion
//#region Post routes
app.post('/register-user', urlenParser, (req, res) => {
const formData = req.body;
if (!serverSideSignUpFormValidate(formData)) {
req.session.err = {
errorMsg: 'Incorrect Username or Password',
errorStatus: true,
};
return res.redirect('/home#modalRegisterForm');
}
bcrypt
.hash(formData.signUpFormPassword, SALT_WORK_FACTOR)
.then((hash) => {
const registeredUserData = new UserModel({
user_name_user: formData.signUpFormUsername,
user_name_fist: formData.signUpFormFirstName,
user_name_last: formData.signUpFormLastName,
email: formData.signUpFormEmail,
user_password: hash,
});
registeredUserData
.save()
.then((response) => {
req.session.user = {
username: registeredUserData.user_name_user,
email: registeredUserData.email,
validState: true,
firstName: registeredUserData.user_name_fist,
lastName: registeredUserData.user_name_last,
isAdmin: registeredUserData.isAdmin,
};
res.redirect('/dashboard');
})
.catch((err) => {
console.log('There was an error registering the user');
console.error(err);
req.session.err = {
errorMsg: 'Username already exists!',
errorStatus: true,
};
res.redirect('/home#modalRegisterForm');
});
})
.catch((err) => {
console.log('Something went wrong, Please try again!');
req.session.err = {
errorMsg: 'Something went wrong, Please sign in Again',
errorStatus: true,
};
res.redirect('/home#modalRegisterForm');
});
});
app.post('/login-user', urlenParser, (req, res) => {
const formData = req.body;
if (!serverSideSignInFormValidate(formData)) {
req.session.err = {
errorMsg: 'Incorrect Username or Password',
errorStatus: true,
};
return res.redirect('/home#modalLoginForm');
}
UserModel.find({
user_name_user: formData.signInFormUsername,
})
.exec()
.then((givenUsers) => {
if (givenUsers.length < 1) {
console.log('User does not exist!');
req.session.err = {
errorMsg: 'Incoorect Username. Please try again',
errorStatus: true,
};
return res.redirect('/home#modalLoginForm');
} else {
bcrypt
.compare(formData.signInFormPassword, givenUsers[0].user_password)
.then((result) => {
if (result) {
console.log('authentication successful');
req.session.user = {
username: givenUsers[0].user_name_user,
email: givenUsers[0].email,
validState: true,
firstName: givenUsers[0].user_name_fist,
lastName: givenUsers[0].user_name_last,
isAdmin: givenUsers[0].isAdmin,
_id: givenUsers[0]._id,
};
UserModel.updateOne(
{
user_name_user: givenUsers[0].user_name_user,
},
{
$set: {
user_visit_last: new Date(),
},
}
).exec();
return res.redirect('/dashboard');
// do other stuff
} else {
console.log("authentication failed. Password doesn't match");
req.session.err = {
errorMsg: 'Incorrect password',
errorStatus: true,
};
return res.redirect('/home#modalLoginForm');
}
})
.catch((err) => {
console.log('Something went wrong, Please try again!');
console.error(err);
req.session.err = {
errorMsg: 'Something went wrong, Please try again!',
errorStatus: true,
};
res.redirect('/home#modalLoginForm');
});
}
})
.catch((err) => {
console.log('Internal server error: ', err);
res.status(500).json({
error: err,
message: 'Error logging in',
});
});
});
app.post('/user/:user_id/follow-user', (req, res) => {
// finding user by id (to whom user is going to follow)
User.findById(req.params.user_id).then((user) => {
//check if follow reqest by user2 is already exist in user1 followers
if (
user.followers.filter(
(follower) => follower.user.toString() === req.session.user._id
).length > 0
) {
return res.status(400).json({
alreadyFollowed: `User already followed ${req.params.user_id}`,
});
}
// the requested user will push and save to followers of other user to whom request has made
user.followers.push(req.session.user._id);
var followedUser = user._id;
user.save();
// we will find current user by email you can find it by _id also
User.findOne({ email: req.session.user.email })
.then((givenUser) => {
// now push the user to following of its own and save the user
givenUser.following.push(followedUser);
user.save().then((u) => res.json(u));
})
.catch((err) =>
console.log(
'error cant follow again you jave already followed the user'
)
);
});
});
//#region Admin Setup // Only run this once and only once
app.get('/firstrunsetup', (req, res) => {
//User that exists -> priyaArasan03121999
//pwd -> Kingan11!!
// user = "smutharasan"
const adminPwd = 'SenecaHackathonCokato4!!';
bcrypt
.hash(adminPwd, SALT_WORK_FACTOR)
.then((hash) => {
const Supriya = new UserModel({
user_name_user: 'priya03121999',
user_password: hash,
user_name_fist: 'Supriya',
user_name_last: 'Mutharasan',
email: '[email protected]',
isAdmin: true,
});
Supriya.save()
.then((response) => {
req.session.user = {
username: Supriya.user_name_user,
email: Supriya.email,
validState: true,
firstName: Supriya.user_name_fist,
lastName: Supriya.user_name_last,
isAdmin: Supriya.isAdmin,
};
res.redirect('/dashboard');
})
.catch((err) => {
console.log('There was an error registering the user');
req.session.err = {
errorMsg: 'Incorrect Username or Password',
errorStatus: true,
};
res.redirect('/home#modalRegisterForm');
});
})
.catch((err) => {
console.log('Something went wrong, Please try again!');
req.session.err = {
errorMsg: 'Something went wrong, Please sign in Again',
errorStatus: true,
};
res.redirect('/home#modalRegisterForm');
});
});
//#endregion
//app.listen(HTTP_PORT, onHttpStart);
server.listen(HTTP_PORT, onHttpStart);