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
|
var express = require('express');
var router = express.Router();
var csrf = require('csurf'); // Cross-Site Request Forgery prevention
var passport = require('passport');
var dbFile = require("../node_simple.js");
var Promise = require('promise');
var csrfProtection = csrf();
router.use(csrfProtection); // router is protected
router.get('/logout', loggedIn, function (req, res, next) {
req.logout();
res.redirect('/');
});
/** Render/GET user_profile page. */
router.get('/user_profile', loggedIn, isUser, function(req, res, next) {
req.session.messages = null;
var comments = [];
var inbox = [];
var error = null;
var follows = [];
function getComments() {
return new Promise(function(resolve, reject) {
dbFile.retrieve_userComments_history(req.user.user_name, function (success, object) {
if (!success) {
// Need to redirect to an error page instead.
if (!comments.length) {
var obj = {
comment: 'Use a heap!',
date: '2016-04-05',
exam_id: 'some id',
course_code : 'CSC263',
term: 'Fall',
year : 2016
};
comments.push(obj);
comments.push(obj);
}
resolve(1);
} else if (object.length){
req.user.comments = object.length;
req.user.comment_list = object;
resolve(1);
} else {
req.user.comments = object.length;
resolve(1);
}
});
});
}
function getMail(){
return new Promise(function(resolve, reject) {
dbFile.checkMailbox(req.user.user_name, function(success, error, data, message){
if (success) {
inbox = data;
req.user.messages = inbox.length;
// needed to display in layout
var i = 0;
inbox.forEach(function(element) {
element.class = i;
i ++;
});
resolve(1);
} else if (error) {
// For testing purposes, remove later:
if (!inbox.length) {
var mail_data = {
sender: 'The Dude', receiver: req.user.user_name,
subject: 'Whoa look at this',
message: 'I sent you a link on Skype',
date: '2016-04-05'
}
}
inbox.push(mail_data);
inbox.push(mail_data);
// Need to redirect to an error page instead.
resolve(1);
}
});
});
}
function getFollows(){
return new Promise(function(resolve, reject) {
dbFile.retrieveFollows(req.user.user_name, function(success, data){
req.user.examfollows = [];
req.user.followerCount = data.length;
if(success && data.length){
var count = 1;
data.forEach(function(item){
dbFile.get_exam_byID(item, function(success, error, data){
if(success){
req.user.examfollows.push(data);
if(count == req.user.followerCount ){
resolve(1);
}else{
count++;
}
}else{
resolve(1);
}
})
});
} else if(success && !data.length){
resolve(1);
}
else{ //error
error = data;
resolve(1);
}
});
});
}
getComments().then(getMail).then(getFollows).then(function (data) {
res.render('user_profile_alt', {inbox: inbox, error: error, csrfToken: req.csrfToken(), userProfile: true});
});
});
/** Render/GET signin page. */
router.get('/signup', loggedOut, function(req, res, next) {
res.render('signup', {csrfToken: req.csrfToken(), success: req.session.success, errors: req.session.errors});
req.session.errors = null;
});
/** Render/GET signup page when the user failed. Redirect to signup page */
router.get('/signup/failed', loggedOut, function(req, res, next) {
var msg = req.flash('error');
res.render('signup', {csrfToken: req.csrfToken(),
success: req.session.success,
errors: req.session.errors,
flashMsg: msg});
});
/** Render/GET signin page. */
router.get('/signin', loggedOut, function (req, res, next) {
var msg = req.flash('error');
res.render('signin', {
csrfToken: req.csrfToken(),
success: req.session.success,
errors: req.session.errors,
flashMsg: msg
});
});
/** Render/GET verification page. */
router.get('/verify', function(req, res, next) {
res.render('verification', {noHeader: true, noFooter: true});
});
/** Retrieves infomation from the signup form, form validates and sends it to passport.js to authenticate. */
router.post('/signup', loggedOut, function(req, res, next) {
req.assert('fname', 'Please enter a valid first name.').notEmpty().withMessage('First name required.').isAlpha();
req.check('lname', 'Please enter a valid first name.').notEmpty().withMessage('Last name required.').isAlpha();
req.check('email', 'Enter a valid Email address').notEmpty().withMessage('Email required').isEmail();
req.check('usrname', 'Enter a valid username').notEmpty().withMessage('Username required.');
req.check('password', "Password should be between 6 and 12 characters.")
.notEmpty().withMessage('Password required').isLength({min: 6, max: 12});
req.check('password', "The confirmation password doesn't match.").equals(req.body.confirmPassword);
// phone number optional
if (req.body.phone_num){
req.check('phone_num', 'Please enter a valid phone number').isMobilePhone('en-CA');
}
var errors = req.validationErrors();
if (errors) {
req.session.errors = errors;
req.session.success = false;
res.redirect('/user/signup');
} else {
passport.authenticate('local_signup', {
successRedirect: '/user/verify',
failureRedirect: '/user/signup/failed',
failureFlash: true
})(req, res);
}
});
/** Retrieves infomation from the signin form, form validates and sends it to passport.js to authenticate. */
router.post('/signin', loggedOut, function(req, res, next) {
req.check('usrname', 'Username field is empty.').notEmpty();
req.check('password', "Password field is empty.").notEmpty();
var errors = req.validationErrors();
if (errors) {
req.session.errors = errors;
req.session.success = false;
res.redirect('/signin');
} else {
passport.authenticate('local_signin', {
successRedirect: '/user/verify',
failureRedirect: '/user/signin',
failureFlash: true
})(req, res);
}
});
/** Retrieves infomation from the message sending form and sends it to the database to add. Redirects back if
* some error occurs. */
router.post('/user_profile/send_message', loggedIn, function(req, res, next) {
if (req.user.user_name === req.body.receiver_username) {
req.session.messages = {error : 'You cannot send a message to yourself.'};
res.redirect('/user/user_profile/');
} else if (!req.body.message){
req.session.messages = {error: 'You cannot send an empty message.'};
res.redirect('/user/user_profile/');
} else {
var date = new Date();
var current_date = date.toString().slice(0, 24);
var subject = req.body.subject;
if (!subject) {
subject = '(none)';
}
var mail_data = {
sender: req.user.user_name,
receiver: req.body.receiver_username,
subject: subject,
message: req.body.message,
date: current_date
};
dbFile.sendMail(mail_data, function(success, error, message) {
if ((!success && !error) || (error)) {
req.session.messages = {error : message};
res.redirect('/user/user_profile/');
} else {
req.session.messages = {success: message};
res.redirect('/user/user_profile/');
//$('#profile-send-message').show();
}
});
}
});
/**Adds a solution into the database, redirect to exam/question/solution page */
router.post('/submit_solution/:examID/:qID', function(req,res){
// Get required fields from body
// Populate this data
/* var Data = {
exam_id: fields[0],
q_id: fields[1],
text: fields[2],
votes: 0,
comments: [],
author: fields[3]
};*/
var examID = req.params.examID;
var qID = req.params.qID;
if (!req.body.solution) {
req.session.messages = {error: 'You cannot submit an empty solution.'};
res.redirect('/user/add_solution/' + examID + '/' + qID);
} else {
var text = req.body.solution,
votes = 0,
comments = [],
author = req.user.user_name;
var fields = [examID, qID, text, author];
// Add to database
dbFile.add_solution(fields, function(addedSolution, statusMsg){
if(addedSolution){
req.session.messages = {success : statusMsg};
}else{
req.session.messages = {error : statusMsg};
}
//Redirect to solutions page again
res.redirect('/solutions/' + examID + '/' + qID);
});
}
});
/** Routed here from Solutions page (add solution button)
* Renders add_solution page for response.*/
router.get('/add_solution/:examID/:qID', function (req, res, next) {
var examID = req.params.examID;
var qID = req.params.qID;
// Must be a logged in user to access
if(req.isAuthenticated()){
res.render('add_solutions', {csrfToken: req.csrfToken(), examID: examID, qID: qID});
} else {
var message = "Must be logged in to add a solution!";
req.session.messages = {error : message};
res.redirect('/solutions/' + examID + '/' + qID);
}
});
/**
* Retrieves information from the comment adding form, authenticates on whether the user is logged in or not (redirects
* in this case). Sends information to the database to add.
*/
router.post('/comment/submit/:examID/:qID/:solID', function(req, res, next){
var examID = req.params.examID;
var qID = req.params.qID;
var comment = req.body.comment;
var username = req.user.user_name;
var solutionID = req.params.solID;
// Must be a logged in user to access
var fields = [comment, username];
if (!req.body.comment ==='') {
req.session.messages = {error : 'Cannot submit an empty comment.'};
res.redirect('/solutions/' + examID + '/' + qID);
} else {
if(req.isAuthenticated()){
dbFile.add_comment(solutionID, fields, function(commentAdded, statusMsg){
if(commentAdded){
req.session.messages = {success : statusMsg};
} else{
req.session.messages = {error : statusMsg};
}
res.redirect('/solutions/' + examID + '/' + qID);
});
} else { //User not logged in
var message = "Must be logged in to comment!";
req.session.messages = {error : message};
res.redirect('/solutions/' + examID + '/' + qID);
}
}
});
/**
* Retrieves information from the voting button and sends it to the database. Redirects back if there is an error, or
* simply shows an error alert if the user is not logged in.
*/
router.post('/solution/vote/:examID/:qID/:solID', function(req, res, next){
var vote = req.body.vote;
var examID = req.params.examID;
var qID = req.params.qID;
var solutionID = req.params.solID;
if(req.isAuthenticated()){
dbFile.vote_solution(solutionID, vote, function(voteCounted, statusMsg){
if(voteCounted){
res.redirect('/solutions/' + examID + '/' + qID);
}else{
req.session.messages = {error : statusMsg};
res.redirect('/solutions/' + examID + '/' + qID);
}
});
} else { //User not logged in
var message = "Must be logged in to Vote!";
req.session.messages = {error : message};
res.redirect('/solutions/' + examID + '/' + qID);
}
});
/**
* Retrieves information about the exam following button, redirects if there is an error, and shows an error alert
* if the user is not logged in.
*/
router.post('/follow_exam/:examID',function (req, res) {
var examId = req.params.examID;
if (req.isAuthenticated()){
dbFile.followExam(req.user.user_name,examId,function (success, message) {
if (success){
req.session.messages = {success : message};
res.redirect('/questions/' + examId);
}else{
req.session.messages = {error : message};
res.redirect('/questions/' + examId);
}
});
}else{
var message = "Must be logged in to follow an exam!";
req.session.messages = {error : message};
res.redirect('/questions/' + examId);
}
});
module.exports = router;
/************** Route protection ********************/
/**
* Redirects to the main page if the user is logged in.
*/
function loggedIn(req, res, next) {
if (req.isAuthenticated()) {
return next();
}
res.redirect('/');
}
/**
* Redirects to the main page is the user is not logged in.
*/
function loggedOut(req, res, next) {
if (!req.isAuthenticated()) {
return next();
}
res.redirect('/');
}
/**
* Redirects to the admin panel if the user is an admin.
*/
function isUser(req, res, next) {
if (req.user && req.user.email) {
return next();
}
res.redirect('/admin');
}
|