diff options
| author | nanalelfe <nargiza.nosirova@mail.utoronto.ca> | 2016-07-22 02:45:27 +0000 |
|---|---|---|
| committer | nanalelfe <nargiza.nosirova@mail.utoronto.ca> | 2016-07-22 02:45:27 +0000 |
| commit | 12659ff5704a04d2447f5dbf036541676c59f139 (patch) | |
| tree | 68a36260059bb05bb9d589011bf9d8ca22f72b2f /routes/index.js | |
| parent | dc3888553bdda4ee30e6440992459916cec8d911 (diff) | |
| parent | 797b4fe83ed4ebbed47930c7d0d0a34cd37703c6 (diff) | |
Added passport for signup
Diffstat (limited to 'routes/index.js')
| -rw-r--r-- | routes/index.js | 141 |
1 files changed, 94 insertions, 47 deletions
diff --git a/routes/index.js b/routes/index.js index c96af41..b998f11 100644 --- a/routes/index.js +++ b/routes/index.js @@ -34,23 +34,62 @@ router.get('/questions', function(req, res, next) { res.render('questions'); }); -/* Render/GET exam page */ -/* Render/GET exam page */ +router.get('/admin', function(req,res){ + res.render('admin', {csrfToken: req.csrfToken()}); +}); + + +router.post('/admin/update', function(req,res){ + var course_code = req.body.course_code, + year = req.body.year, + type = req.body.type, + term = req.body.term, + instructors = parseStringArray(req.body.instructors), + page_count = req.body.page_count, + questions_count = req.body.questions_count, + questions_list = parseStringArray(req.body.questions_list), + upload_date = req.body.upload_date, + uploaded_by = req.body.uploaded_by; + + var fields = [ + course_code, // String + year, // Int + type, // String; Needs to be added to database code + term, // String + instructors, // Array of strings + page_count, // Int + questions_count, // Int + upload_date, // String + uploaded_by]; // String + + dbFile.add_exam(fields, questions_list, function(examAdded, statusMessage){ + if(examAdded){ + console.log("Success!"); + }else{ + console.log("Failed!"); + } + console.log(statusMessage); + }); + res.redirect('/admin'); +}); + + //EXAMPLE EXPECTED DATA GIVEN BELOW: -/*[ { courseCode: 'CSC240', - year: 2016, - term: 'Fall', - instructors: [ 'Faith Ellen', 'Tom F.' ], - type: 'Midterm Examination', - title: 'Thry of Computation' }, - { courseCode: 'CSC240', - year: 2014, - term: 'Fall', - instructors: [ 'Faith Ellen', 'Tom F.' ], - type: 'Midterm Examination', - title: 'Thry of Computation' } ] - */ +/*[ {courseCode: 'CSC240', + year: 2016, + term: 'Fall', + instructors: ['Faith Ellen', 'Tom F.'], + type: 'Midterm Examination', + title: 'Thry of Computation' }, + + {courseCode: 'CSC240', + year: 2014, + term: 'Fall', + instructors: ['Faith Ellen', 'Tom F.'], + type: 'Midterm Examination', + title: 'Thry of Computation' } ] + */ router.get('/exams/:id', function(req, res, next) { var minExamInfoArray = []; dbFile.get_all_exams(req.params.id, function (exams) { @@ -61,6 +100,7 @@ router.get('/exams/:id', function(req, res, next) { //console.log(exams); //only pass over the information that is necessary for the exams page for (var i = 0; i<exams.length;i++){ + var getInstructors = exams[i].instructors.join(", "); var minExamInfo = { courseCode:exams[i].course_code, year:exams[i].year, @@ -86,14 +126,6 @@ router.get('/search', function(req, res, next) { res.redirect('/exams/' + courseName); }); - -/* REDIRECT - exam -> questions page*/ -router.get('/exam_click',function (req,res) { - //TODO:get needs to have exam_id attached - var examId = req.query.exam_id; - res.redirect('/questions/'+examId); -}); - /*EXAMPLE DATA GIVEN BELOW: * questions = [{id,count,comments},{id,count,comments}] --> array of "question" objects * @@ -101,9 +133,10 @@ router.get('/exam_click',function (req,res) { * count= number of solutions * comments = number of comments*/ router.get('/questions/:exam_id', function (req,res) { + console.log(req.params.exam_id); dbFile.get_exam_info_by_ID(req.params.exam_id, function (questions) { - res.render('questions', {query: questions}); + console.log(questions); }); }); @@ -111,22 +144,36 @@ router.get('/signup', function(req, res, next) { res.render('signup', {csrfToken: req.csrfToken(), success: req.session.success, errors: req.session.errors}); }); -router.post('/signup', function(req, res, next) { + +router.post('/signup', passport.authenticate('local_signup', { + successRedirect: '/user_profile', + failureRedirect: '/signup', + failureFlash: true +})); + +/*router.post('/signup', function(req, res, next) { req.check('email', 'Invalid email address').isEmail(); req.check('password', "Password is invalid").isLength({min: 4}).equals(req.body.confirmPassword); // password has to be at least 4 characters long var errors = req.validationErrors(); if (errors) { - console.log(req); req.session.errors = errors; req.session.success = false; + console.log("got here"); + res.redirect('/signup'); } else { req.session.success = true; + console.log("GOT SUCCESS"); + passport.authenticate('local_signup', { + successRedirect: '/user_profile', + failureRedirect: '/signup', + failureFlash: true + }); } - res.redirect('/signup'); + //res.redirect('/signup'); -}); +});*/ /*router.post('/signup', passport.authenticate('local.signup', { sucessRedirect: '/profile', @@ -134,21 +181,7 @@ router.post('/signup', function(req, res, next) { failureFlash: true }));*/ - -/* -app.get('/exams',function (req,res) { - console.log(req.query.search); - var courseName = req.query.search; - res.redirect("http://localhost:3000/exams.html/?course_name="+courseName); -}); - - -app.get('/exams.html', function (req,res) { - /!*LOADS ALL THE STATIC FILES REALTIVE TO THE REDIRECTED URL*!/ - app.use('/exams.html', express.static(__dirname)); - res.sendFile(__dirname+'/exams.html'); -});*/ - +/**** Helpers ****/ function getExamsForCourseCode(courseCode) { dbFile.get_all_exams(courseCode, function (exams) { @@ -161,9 +194,23 @@ function getExamsForCourseCode(courseCode) { }); } - -module.exports = router; - function toProperCase(string) { return string.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();}); -}
\ No newline at end of file +} + + +/* Takes an input string delimited by commas, will split by comma and trim white + * spaces. Consider callback. + */ +function parseStringArray(input){ + var list = input.split(','); + var parsedList = []; + list.forEach(function(word){ + if (word != ""){ + parsedList.push(word.trim()); + } + }); + return parsedList; +} + +module.exports = router;
\ No newline at end of file |
