// USER DEACTIVATE ACCOUNT app.post('/user/deactivate-account', async (req, res) => { let { id } = req.session, userPassword = await User.getWhat('password', id), { password } = req.body, samePassword = await User.comparePassword(password, userPassword) req.checkBody('password', 'Password is empty!!').notEmpty() let errors = await req.getValidationResult() if (!errors.isEmpty()) { let array = [] errors.array().forEach(e => array.push(e.msg)) res.json({ mssg: array }) } else if (!samePassword) { res.json({ mssg: 'Wrong password!!' }) } else { await User.deactivate(id, req, res) res.json({ mssg: 'Deactivated your account successfully!!', success: true, }) } })
db.c_validator('surname', req) req.checkBody('email', 'Email is empty').notEmpty() req.checkBody('email', 'Email is invalid').isEmail()
{ id } = req.session req.checkBody('name', 'Name is empty!!').notEmpty() req.checkBody('name', 'Name must be less than 255!!').isLength({ max: 255 })
req.checkBody('email', 'Email is empty!!').notEmpty() req.checkBody('email', 'Invalid email!!').isEmail()
/* Handle Registration POST */ router.post('/signup', function(req, res, next) { req.checkBody('username', 'Invalid username').notEmpty().isAlphanumeric(); req.checkBody('password', 'Invalid password').notEmpty().isLength({ min: 5 }); req.getValidationResult().then(function(result) { if (!result.isEmpty()) { req.flash('message', 'Invalid input.'); return res.redirect('/signup'); } passport.authenticate('signup', { successRedirect: '/', failureRedirect: '/signup', failureFlash: true })(req, res, next); }); });
/* Handle Login POST */ router.post('/login', function(req, res, next) { req.checkBody('username', 'Invalid username').notEmpty(); req.checkBody('password', 'Invalid password').notEmpty(); req.getValidationResult().then(function(result) { if (!result.isEmpty()) { req.flash('message', 'Invalid input.'); return res.redirect('/login'); } var redirectTo = req.session.redirectTo ? req.session.redirectTo : '/'; delete req.session.redirectTo; passport.authenticate('login', { successRedirect: redirectTo, failureRedirect: '/login', failureFlash: true })(req, res, next); }); });
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); } }) }));
{ id: session } = req.session req.checkBody('username', 'Username is empty').notEmpty() req.checkBody('username', 'Username must contain only leters').isAlpha() req.checkBody('username', 'Username must be greater than 4').isLength({ min: 4 }) req.checkBody('username', 'Username must be less than 32').isLength({ max: 32 }) req.checkBody('email', 'Email is empty').notEmpty() req.checkBody('email', 'Email is invalid').isEmail()
// 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); });