aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHumairAK <humair88@hotmail.com>2016-07-22 08:20:42 +0000
committerHumairAK <humair88@hotmail.com>2016-07-22 08:20:42 +0000
commitd05d94d5c4afc01ca7ade508d84af2b6f318d673 (patch)
treee36daf64f6a901fcc1228bb198ec736952bc3e73
parent92c187a6a79e5e45dcfd41ff4f711b4beadcc252 (diff)
parent2a7e01b63e58db7ce0ef19210aef9874e022a7c5 (diff)
fixed conflict in passport.js
-rw-r--r--config/passport.js23
-rw-r--r--node_simple.js30
-rw-r--r--routes/index.js47
-rw-r--r--views/partials/header.hbs2
-rw-r--r--views/signin.hbs38
-rw-r--r--views/signup.hbs4
6 files changed, 128 insertions, 16 deletions
diff --git a/config/passport.js b/config/passport.js
index 4bc53d6..dff3100 100644
--- a/config/passport.js
+++ b/config/passport.js
@@ -57,3 +57,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 ba20ba1..a685cf0 100644
--- a/node_simple.js
+++ b/node_simple.js
@@ -136,7 +136,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.");
db.close();
@@ -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
* */
exports.find_user = function (email, callbackUser, callback) {
diff --git a/routes/index.js b/routes/index.js
index a15e48e..e4a841e 100644
--- a/routes/index.js
+++ b/routes/index.js
@@ -172,15 +172,20 @@ router.get('/signup/failed', 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();
@@ -195,16 +200,34 @@ router.post('/signup', function(req, res, next) {
failureRedirect: '/signup/failed',
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 d14b1f6..b7f3600 100644
--- a/views/signup.hbs
+++ b/views/signup.hbs
@@ -42,14 +42,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">