2020-04-18 00:14:31 +00:00
|
|
|
const {check, body, validationResult} = require('express-validator');
|
2020-01-15 21:52:01 +00:00
|
|
|
|
|
|
|
const signupValidationRules = () => {
|
|
|
|
return [
|
|
|
|
check('email', 'invalid email').normalizeEmail().isEmail(),
|
|
|
|
check('username', 'invalid username').isString(),
|
|
|
|
check('password', 'invalid password').isString().isLength({min: 8}),
|
2020-01-16 04:04:55 +00:00
|
|
|
check('confirmPassword', 'invalid password').isString()
|
|
|
|
.custom((confirmPassword, { req }) => confirmPassword === req.body.password),
|
2020-04-18 00:14:31 +00:00
|
|
|
body('username').escape()
|
2020-01-15 21:52:01 +00:00
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
const loginValidationRules = () => {
|
|
|
|
return [
|
|
|
|
check('username', 'bad credentials').isString(),
|
|
|
|
check('password', 'bad credentials').isString()
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
const validate = (req, res, next) => {
|
2020-01-16 04:04:55 +00:00
|
|
|
const errors = validationResult(req);
|
2020-01-15 21:52:01 +00:00
|
|
|
if (errors.isEmpty()) {
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
const extractedErrors = []
|
|
|
|
errors.array().map(err => extractedErrors.push({ [err.param]: err.msg }))
|
|
|
|
return res.status(422).json({
|
|
|
|
errors: extractedErrors,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
signupValidationRules,
|
|
|
|
loginValidationRules,
|
|
|
|
validate
|
|
|
|
}
|