app.post('/askEmail', (req, res) => { console.log('POST EMAIL') req.assert('email', 'Email is not valid').isEmail(); var errors = req.validationErrors(); if (errors) { console.log('error', errors) req.flash('errors', errors); return res.redirect('/askEmail'); } const body = req.body req.user.askEmail = false req.user.email = body.email req.user.save((err, saved) => { console.log(err, saved) res.redirect('/') }) })
app.get('/users/:userId', function (req, res) { req.assert('userId').notEmpty(); var errors = req.validationErrors(); if (errors) { res.send({ error: -1, data: errors }); return; } var userId = req.params.userId; User.find({ where: { userId: userId } }).success(function (user) { var data = {}; data.userId = user.userId; data.topScore = user.topScore; data.coin = user.coin; data.itemSlot1 = user.itemSlot1; data.itemSlot2 = user.itemSlot2; data.itemSlot3 = user.itemSlot3; res.send({ error: '', data: data }); }).error(function (error) { res.send({ error: 'db error' }); }); });
app.post("/submit", (req, res) => { req.check("email", "Invalid Email").isEmail(); // isMobilePhone // isEmail() // isPostalCode // isCurrency // isCreditCard req .check("password", "Invalid Password") .isLength({ min: 6 }) .equals(req.body.confPassword); const errors = req.validationErrors(); if (errors) { req.session.errors = errors; req.session.done = false; } else { req.session.done = true; } res.redirect("/"); });
req.assert('password','Enter a password 6 - 20').len(6,20); var errors = req.validationErrors(); if(errors){ res.status(422).json(errors);
router.post('/account', function (req, res, next) { const body = req.body; var name = body.name ? body.name : req.user.name; var email = body.email ? body.email : req.user.email; if (body.email) req.assert('email', 'Email is not valid').isEmail(); const errors = req.validationErrors(); if (errors) { req.flash('errors', errors); return res.redirect('/user/account'); } User.update({ _id: req.user._id }, { profile: { name: name }, email: email }, function (err, doc) { if (err) { req.flash('errors', { msg: 'Error: Could not update.' }); return res.redirect('/user/account'); } else { req.flash('success', { msg: 'Success! Account updated!' }); return res.redirect('/user/account') } }) })
var errors = req.validationErrors(); if(errors){ var messages = [];
// Account login app.post('/account/login', function(req,res){ // Validation prior to checking DB. Front end validation exists, but this functions as a fail-safe req.checkBody('username', 'Username is required').notEmpty(); req.checkBody('password', 'Password is required').notEmpty(); var errors = req.validationErrors(); // returns an object with results of validation check if (errors) { res.status(401).send('Username or password was left empty. Please complete both fields and re-submit.'); return; } // Create session if username exists and password is correct passport.authenticate('local', function(err, user) { if (err) { return next(err); } if (!user) { return res.status(401).send('User not found. Please check your entry and try again.'); } req.logIn(user, function(err) { // creates session if (err) { return res.status(500).send('Error saving session.'); } var userInfo = { username: user.username, name : user.name, email : user.email }; return res.json(userInfo); }); })(req, res); });
req.assert('password','Enter a password 6 - 20').len(6,20); var errors = req.validationErrors(); if(errors){ res.status(422).json(errors);
router.post('/products', (req, res, next) => { req.checkBody('title', 'Enter a valid email address').isEmail(); var errors = req.validationErrors(); if(errors){ res.send(errors); } else{ var p = new Product(); p.title = req.body.title; p.price = req.body.price; p.instock = req.body.instock; p.photo = req.body.photo; p.save((err, data) => { if(err) { res.json({ data: {}, mes: `Error . ${err}` }); } else{ res.json({ data: data, mes: 'Successfuly' }); } }); } });
var errors = req.validationErrors();
req.assert('password', 'Password must be at least 6 characters long').len(6); var errors = req.validationErrors();
let errors = req.validationErrors();
passport.use('local.signin', new LocalStartergy({ usernameField: 'email', passwordField: 'password', passReqToCallback: true }, function (req, email, password, done) { req.checkBody('email', 'Invalid Email').notEmpty().isEmail(); req.checkBody('password', 'Invalid Password').notEmpty(); var errors = req.validationErrors(); if (errors) { var messages = []; errors.forEach(function (error) { messages.push(error.msg) }); return done(null, false, req.flash('error', messages)); } User.findOne({'email': email}, function (err, user) { if (err) { return done(err); } else { if (!user) { return done(null, false, {message: 'No user found!'}); } if (!user.validPassword(password)) { return done(null, false, {message: 'Wrong Password!'}); } return done(null, user); } }) }));
var errors = req.validationErrors();
req.assert('password','Enter a password 6 - 20').len(6,20); var errors = req.validationErrors(); if(errors){ res.status(422).json(errors);