diff options
| -rw-r--r-- | config/passport.js | 41 | ||||
| -rw-r--r-- | routes/admin.js | 21 | ||||
| -rw-r--r-- | views/signin_admin.hbs | 2 |
3 files changed, 48 insertions, 16 deletions
diff --git a/config/passport.js b/config/passport.js index 3c99b10..bff5200 100644 --- a/config/passport.js +++ b/config/passport.js @@ -104,21 +104,8 @@ passport.use('local_signin', new LocalStrategy({ } else if (!success && !error) { // Username is undefined - //return done(null, false, {message: message}); - // ADMIN - dbFile.adminExists(usrname, function (error, exists, data, message) { - if (error) { - return done(message); - } else if (!error && !exists){ - return done(null, false, {message: 'Username does not exist.'}); - } else { - if (comparePassword(password, data.password)) { - return done(null, data); - } else { - return done(null, false, {message: 'Password incorrect.'}); - } - } - }); + + return done(null, false, {message: 'Username does not exist.'}); } else { @@ -141,6 +128,30 @@ passport.use('local_signin', new LocalStrategy({ /** + * Configures the Local strategy to authenticate the Admin signin request. + */ +passport.use('admin_signin', new LocalStrategy({ + usernameField: 'usrname', + passwordField: 'password', + passReqToCallback: true +}, function (req, usrname, password, done) { + dbFile.adminExists(usrname, function (error, exists, data, message) { + if (error) { + return done(message); + } else if (!error && !exists){ + return done(null, false, {message: 'Username does not exist.'}); + } else { + if (comparePassword(password, data.password)) { + return done(null, data); + } else { + return done(null, false, {message: 'Password incorrect.'}); + } + } + }); +})); + + +/** * Configures the Facebook strategy to authenticate signin as well as signup request. * * var user_data = { diff --git a/routes/admin.js b/routes/admin.js index 6a628d2..425dcc0 100644 --- a/routes/admin.js +++ b/routes/admin.js @@ -52,6 +52,27 @@ router.get('/signin', function (req, res, next) { }); }); +/** Retrieves infomation from the signin form, form validates and sends it to passport.js to authenticate. */ +router.post('/signin', 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('/admin/signin'); + } else { + passport.authenticate('admin_signin', { + successRedirect: '/admin/', + failureRedirect: '/admin/signin', + failureFlash: true + })(req, res); + } + + +}); + /** Retrieves infomation from the exam adding form and sends it to the database to add. */ router.post('/add/exam', function(req,res){ req.sanitize('course_code').escape(); diff --git a/views/signin_admin.hbs b/views/signin_admin.hbs index 35d64fe..92adcad 100644 --- a/views/signin_admin.hbs +++ b/views/signin_admin.hbs @@ -25,7 +25,7 @@ {{/if}} <h3 class="signin"><hr><i class="fa fa-sign-in" aria-hidden="true"></i> Admin Sign In<hr></h3> - <form action="/user/signin" method="post"> + <form action="/admin/signin" method="post"> <div class="form-group signin-group"> <div class="input-group input-group-lg"> <div class="input-group-addon addon-username"> |
