Skip to content

Commit

Permalink
Setup email and password validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Brendon1911 committed Sep 13, 2018
1 parent a45b17e commit 10c8aac
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 0 deletions.
2 changes: 2 additions & 0 deletions shopping-cart/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var mongoose = require('mongoose');
var session = require('express-session');
var passport = require('passport');
var flash = require('connect-flash');
var validator = require('express-validator');

var indexRouter = require('./routes/index');

Expand All @@ -23,6 +24,7 @@ app.set('view engine', '.hbs');
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(validator());
app.use(cookieParser());
app.use(session({ secret: 'mySuperSecretSecret', resave: false, saveUninitialized: false }));
app.use(flash());
Expand Down
10 changes: 10 additions & 0 deletions shopping-cart/config/passport.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ passport.use('local.signup', new LocalStrategy({
passwordField: 'password',
passReqToCallback: true
}, (req, email, password, done) => {
req.checkBody('email', 'Invalid email').isEmail();
req.checkBody('password', 'Invalid password').isLength({ min: 4 });
var errors = req.validationErrors();
if (errors) {
messages = [];
errors.forEach((error) => {
messages.push(error.msg);
});
return done(null, false, req.flash('error', messages));
}
User.findOne({'email': email}, (err, user) => {
if (err) {
return done(err);
Expand Down
Binary file modified shopping-cart/data/local.0
Binary file not shown.
Binary file modified shopping-cart/data/local.ns
Binary file not shown.
14 changes: 14 additions & 0 deletions shopping-cart/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions shopping-cart/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"express": "~4.16.0",
"express-handlebars": "^3.0.0",
"express-session": "^1.15.6",
"express-validator": "^5.3.0",
"hbs": "~4.0.1",
"http-errors": "~1.6.2",
"mongoose": "^5.2.14",
Expand Down

0 comments on commit 10c8aac

Please sign in to comment.