From 2a7e01b63e58db7ce0ef19210aef9874e022a7c5 Mon Sep 17 00:00:00 2001 From: nanalelfe Date: Fri, 22 Jul 2016 04:18:26 -0400 Subject: Added validation checks for signup --- config/passport.js | 24 ++++++++++++++++++++++++ node_simple.js | 30 +++++++++++++++++++++++++++++- routes/index.js | 47 +++++++++++++++++++++++++++++++++++------------ views/partials/header.hbs | 2 +- views/signin.hbs | 38 ++++++++++++++++++++++++++++++++++++++ views/signup.hbs | 4 ++-- 6 files changed, 129 insertions(+), 16 deletions(-) create mode 100644 views/signin.hbs diff --git a/config/passport.js b/config/passport.js index 9709693..219ead6 100644 --- a/config/passport.js +++ b/config/passport.js @@ -41,6 +41,7 @@ passport.use('local_signup', new LocalStrategy({ if (!success && error) { console.log("!success && error"); + console.log(message); return done(message); } @@ -57,3 +58,26 @@ passport.use('local_signup', new LocalStrategy({ }); })); + +passport.use('local_signin', new LocalStrategy({ + usernameField: 'usrname', + passwordField: 'password', + passReqToCallback: true +}, function (req, usrname, password, done) { + + dbFile.retrieveUser(usrname, function (success, error, user, message) { + if (!success && error) { + return done(message); + } + + else if (!success && !error) { + console.log(message); + return done(null, false, {message: message}); + } + + else { + console.log(user[0]); + return done(null, user[0]); + } + }); +})); diff --git a/node_simple.js b/node_simple.js index 5c0666b..fa46447 100644 --- a/node_simple.js +++ b/node_simple.js @@ -137,7 +137,7 @@ exports.add_user = function (fields, callbackUser) { } else {// user insert successfull - logins.insertOne( login_data, function (err) { + logins.insertOne(login_data, function (err) { if (err) { callbackUser(false, true, "Error : User has not been added."); } @@ -193,6 +193,34 @@ exports.find_user_name = function (user_name, callbackUser, callback) { }) }; +/* + Retrieves the user object based on the username. +*/ + +exports.retrieveUser = function (username, callback) { + mongoFactory.getConnection(uri).then(function (db) { + var users = db.collection('users'); + + users.find({user_name: username}).toArray(function (err, result) { + if (err) { + // callback(success, error, user, message) + callback(false, true, null, "Error : Could not retrieve user."); + } + + else if (result.length) { + callback(true, false, result, "User retrieved"); + } + + else { + callback(false, false, null, "Username is undefined."); + } + + }); + }); +} + + + /* * This (helper) function returns true IFF email already exists in the database * */ diff --git a/routes/index.js b/routes/index.js index b6db0ca..cd527b7 100644 --- a/routes/index.js +++ b/routes/index.js @@ -160,15 +160,20 @@ router.get('/signup', function(req, res, next) { }); -/*router.post('/signup', passport.authenticate('local_signup', { - successRedirect: '/user_profile', - failureRedirect: '/signup', - failureFlash: true -}));*/ +router.get('/signin', function (req, res, next) { + res.render('signin', {csrfToken: req.csrfToken(), success: req.session.success, errors: req.session.errors}); +}); + router.post('/signup', function(req, res, next) { - req.check('email', 'Invalid email address').isEmail(); - req.check('password', "Password is invalid").isLength({min: 6}).equals(req.body.confirmPassword); + 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('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); + req.check('phone_num', 'Please enter a valid phone number').optional().isMobilePhone('en-CA'); // password has to be at least 4 characters long var errors = req.validationErrors(); @@ -183,16 +188,34 @@ router.post('/signup', function(req, res, next) { failureRedirect: '/signup', failureFlash: true })(req, res); + } //res.redirect('/signup'); }); -/*router.post('/signup', passport.authenticate('local.signup', { - sucessRedirect: '/profile', - failureRedirect: '/signup', - failureFlash: true -}));*/ +router.post('/signin', function(req, res, next) { + req.check('usrname', 'Username field is empty.').notEmpty(); + req.check('password', "Password field is empty.").notEmpty(); + // password has to be at least 4 characters long + + var errors = req.validationErrors(); + if (errors) { + req.session.errors = errors; + req.session.success = false; + res.redirect('/signin'); + } else { + console.log("GOT SUCCESS"); + passport.authenticate('local_signin', { + successRedirect: '/user_profile', + failureRedirect: '/signin', + failureFlash: true + })(req, res); + + } + //res.redirect('/signup'); + +}); /**** Helpers ****/ diff --git a/views/partials/header.hbs b/views/partials/header.hbs index 8298ce2..4e7f1c2 100644 --- a/views/partials/header.hbs +++ b/views/partials/header.hbs @@ -36,7 +36,7 @@
  • Fourth Year
  • -
  • Log In
  • +
  • Sign In
  • Admin Panel
  • Sign up
  • diff --git a/views/signin.hbs b/views/signin.hbs new file mode 100644 index 0000000..dd82338 --- /dev/null +++ b/views/signin.hbs @@ -0,0 +1,38 @@ +
    + +
    + +
    + {{# if success }} +
    + +

    Signup Successful!

    +
    + {{ else }} + {{# if errors }} +
    +
      + {{# each errors }} +
    • {{ this.msg }}
    • + {{/each}} +
    +
    + {{/if}} + {{/if}} + +

    Sign In

    +
    +
    + +
    +
    + +
    + + +
    +
    + +
    + +
    \ No newline at end of file diff --git a/views/signup.hbs b/views/signup.hbs index 24fad1f..ced61e6 100644 --- a/views/signup.hbs +++ b/views/signup.hbs @@ -35,14 +35,14 @@
    - +
    - +
    -- cgit v1.2.3