diff options
| author | nanalelfe <nargiza.nosirova@mail.utoronto.ca> | 2016-07-22 09:05:04 +0000 |
|---|---|---|
| committer | nanalelfe <nargiza.nosirova@mail.utoronto.ca> | 2016-07-22 09:06:06 +0000 |
| commit | 7d492571ff92dc5b5ac963ab156d27ef5b51f782 (patch) | |
| tree | 8b794607d5b218dfd574a1cedfaf5597136c06c5 | |
| parent | 794c7c94d35d096bbb62a569da73ddeeb4a49c52 (diff) | |
Fixed login password check. Created new function retrievePassword in node_simple.js
| -rw-r--r-- | config/passport.js | 16 | ||||
| -rw-r--r-- | node_simple.js | 19 | ||||
| -rw-r--r-- | routes/index.js | 5 | ||||
| -rw-r--r-- | views/signin.hbs | 5 |
4 files changed, 41 insertions, 4 deletions
diff --git a/config/passport.js b/config/passport.js index dff3100..71e8a15 100644 --- a/config/passport.js +++ b/config/passport.js @@ -10,7 +10,7 @@ var encryptPassword = function (password) { return bcrypt.hashSync(password, bcrypt.genSaltSync(5), null); } -var comparePassword = function (password) { +var comparePassword = function (password1, password2) { return bcrypt.compareSync(password1, password2); } @@ -76,7 +76,19 @@ passport.use('local_signin', new LocalStrategy({ else { console.log(user[0]); - return done(null, user[0]); + dbFile.retrievePassword(usrname, function(success, hash_pwd, message) { + if (!success) { + return done(message); + } else { + console.log(hash_pwd); + if (comparePassword(password, hash_pwd)) { + + return done(null, user[0]); + } else { + return done(null, false, {message: 'Password incorrect.'}); + } + } + }); } }); })); diff --git a/node_simple.js b/node_simple.js index a685cf0..f735b82 100644 --- a/node_simple.js +++ b/node_simple.js @@ -218,7 +218,26 @@ exports.retrieveUser = function (username, callback) { }); } +/* + Returns the hashed password given the username. Assume username exists. + */ + +exports.retrievePassword = function (username, callback) { + mongoFactory.getConnection(uri).then(function (db) { + + db.collection('logins').find({user_name: username}).toArray(function(err, result) { + if (err) { + // callback(success, password, message) + callback(false, null, "Error : Could not retrieve password."); + } + else { + var pwd = result[0].password; //result is an array + callback(true, pwd, "Password retrieved"); + } + }); + }); +} /* * This (helper) function returns true IFF email already exists in the database diff --git a/routes/index.js b/routes/index.js index 56401de..51e1cd2 100644 --- a/routes/index.js +++ b/routes/index.js @@ -171,7 +171,8 @@ router.get('/signup/failed', function(req, res, next) { router.get('/signin', function (req, res, next) { - res.render('signin', {csrfToken: req.csrfToken(), success: req.session.success, errors: req.session.errors}); + var msg = req.flash('error'); + res.render('signin', {csrfToken: req.csrfToken(), success: req.session.success, errors: req.session.errors, flashMsg: msg}); }); @@ -179,7 +180,7 @@ router.post('/signup', 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.').isAlphanumeric(); + 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); diff --git a/views/signin.hbs b/views/signin.hbs index dd82338..76b55f8 100644 --- a/views/signin.hbs +++ b/views/signin.hbs @@ -19,6 +19,11 @@ </section> {{/if}} {{/if}} + {{#if flashMsg}} + <section class="alert alert-danger"> + {{flashMsg}} + </section> + {{/if}} <h2><i class="fa fa-sign-in" aria-hidden="true"></i> Sign In</h2> <form action="/signin" method="post"> |
