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
|
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 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) {
// GET COMMENTS
var comments = [];
dbFile.retrieve_userComments_history(req.user.user_name, function (success, object) {
if (!success) {
// Need to redirect to an error page instead.
console.log("Could not retrieve comments.");
} else if (object){
comments = object;
for (var comment in comments) {
dbFile.get_exam_byID(comment.exam_id, function(success, error, data) {
if (success) {
comment.exam_info = {
course_code : data.course_code,
term: data.term,
year : data.year
};
}
});
}
}
});
// For testing purposes, remove later:
if (!comments.length) {
var obj = {
comment: 'Use a heap!',
date: '2016-04-05',
exam_id: 'some id',
exam_info : {
course_code : 'CSC263',
term: 'Fall',
year : 2016
}
};
comments.push(obj);
comments.push(obj);
}
res.render('user_profile_alt', {comments : comments});
});
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;
});
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});
});
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
});
});
router.post('/signup', loggedOut, function(req, res, next) {
req.check('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 {
console.log("GOT SUCCESS");
passport.authenticate('local_signup', {
successRedirect: '/user/user_profile',
failureRedirect: '/user/signup/failed',
failureFlash: true
})(req, res);
}
});
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/user_profile',
failureRedirect: '/user/signin',
failureFlash: true
})(req, res);
}
});
module.exports = router;
/************** Route protection ********************/
function loggedIn(req, res, next) {
if (req.isAuthenticated()) {
return next();
}
res.redirect('/');
}
function loggedOut(req, res, next) {
if (!req.isAuthenticated()) {
return next();
}
res.redirect('/');
}
function isUser(req, res, next) {
if (req.user.email) {
return next();
}
res.redirect('/admin');
}
|