diff options
| author | nanalelfe <nargiza.nosirova@mail.utoronto.ca> | 2016-07-22 08:18:26 +0000 |
|---|---|---|
| committer | nanalelfe <nargiza.nosirova@mail.utoronto.ca> | 2016-07-22 08:18:26 +0000 |
| commit | 2a7e01b63e58db7ce0ef19210aef9874e022a7c5 (patch) | |
| tree | ce22bfbbf5e9f908d8aa10817549c93bb4a05d30 | |
| parent | e0da9feb10efd002f6ebaa99870b4317687a4557 (diff) | |
Added validation checks for signup
| -rw-r--r-- | config/passport.js | 24 | ||||
| -rw-r--r-- | node_simple.js | 30 | ||||
| -rw-r--r-- | routes/index.js | 47 | ||||
| -rw-r--r-- | views/partials/header.hbs | 2 | ||||
| -rw-r--r-- | views/signin.hbs | 38 | ||||
| -rw-r--r-- | views/signup.hbs | 4 |
6 files changed, 129 insertions, 16 deletions
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."); } @@ -194,6 +194,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 * */ exports.find_user = function (email, callbackUser, callback) { 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 @@ <li><a href="#">Fourth Year</a></li> </ul> </li> - <li><a href="#" data-toggle="modal" data-target=".login-window">Log In</a></li> + <li><a href="/signin" >Sign In</a></li> <li><a href="/admin">Admin Panel</a></li> <!--<li><a href="#" data-toggle="modal" data-target=".registration-window">Sign up</a></li>--> <li><a href="/signup" >Sign up</a></li> 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 @@ +<main id="solutions-main"> + + <div class="row" id="solutions-main"> + + <div class="col-md-4 col-md-offset-4 col-sm-6 col-sm-offset-3"> + {{# if success }} + <section class="alert alert-success"> + <!-- Change those to banners later --> + <h2>Signup Successful!</h2> + </section> + {{ else }} + {{# if errors }} + <section class="alert alert-danger"> + <ul> + {{# each errors }} + <li>{{ this.msg }}</li> + {{/each}} + </ul> + </section> + {{/if}} + {{/if}} + + <h2><i class="fa fa-sign-in" aria-hidden="true"></i> Sign In</h2> + <form action="/signin" method="post"> + <div class="form-group"> + <input type="text" class="form-control" placeholder="New Username" name="usrname"> + </div> + <div class="form-group"> + <input type="password" class="form-control" placeholder="New Password" name="password"> + </div> + <input type="hidden" name="_csrf" value="{{ csrfToken }}"> + <button type="signin" class="btn btn-primary">Sign In</button> + </form> + </div> + + </div> + +</main>
\ 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 @@ </div> <div class="form-group"> - <input type="email" class="form-control" placeholder="New Username" name="usrname"> + <input type="text" class="form-control" placeholder="New Username" name="usrname"> </div> <div class="form-group"> <input type="password" class="form-control" placeholder="New Password" name="password"> </div> <div class="form-group"> - <input type="password" class="form-control" placeholder="New Password" name="confirmPassword"> + <input type="password" class="form-control" placeholder="Confirm Password" name="confirmPassword"> </div> <div class="form-group"> <input type="number" class="form-control" placeholder="Phone Number (optional)" name="phone_num"> |
