-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(validation): move validators into middleware
Tell express app to use validator library
- Loading branch information
Showing
4 changed files
with
123 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,96 +1,26 @@ | ||
import { body, validationResult } from 'express-validator/check'; | ||
import { sanitizeBody } from 'express-validator/filter'; | ||
import jwt from 'jsonwebtoken'; | ||
// import debug from 'debug'; | ||
|
||
import users from '../utils/sample.users'; | ||
// import dev_logger from '../utils/loggers'; | ||
|
||
const AuthenticationMiddleware = { | ||
generateToken: [ | ||
|
||
body('email') | ||
.isEmail() | ||
.withMessage('Please provide a valid email address') | ||
.normalizeEmail(), | ||
body('password') | ||
.not().isEmpty().withMessage('Password is required') | ||
.isLength({ min: 8 }).trim() | ||
.withMessage('Password must be at least 8 characters') | ||
.isLength({ max: 16 }) | ||
.withMessage('Password must be at most 16 characters') | ||
.isAlphanumeric().withMessage('Password must be alphanumeric') | ||
.custom((value, { req }) => { | ||
if (value !== req.body.confirm_password) { | ||
throw new Error( | ||
'Password confirmation does not match password' | ||
); | ||
} | ||
else return value; | ||
}), | ||
sanitizeBody('email').trim().escape(), | ||
sanitizeBody('password').trim().escape(), | ||
sanitizeBody('confirm_password').trim().escape(), | ||
|
||
(req, res, next) => { | ||
|
||
const errors = validationResult(req); | ||
if (!errors.isEmpty()) { | ||
return res.status(422).json({ errors: errors.array() }); | ||
} | ||
|
||
req.token = jwt.sign( | ||
req.body, | ||
process.env.JWT_SECRET, | ||
{ expiresIn: '24h' } | ||
); | ||
generateToken: (req, res, next) => { | ||
req.token = jwt.sign( | ||
req.body, | ||
process.env.JWT_SECRET, | ||
{ expiresIn: '24h' } | ||
); | ||
return next(); | ||
}, | ||
|
||
verifyToken: (req, res, next) => { | ||
try { | ||
req.user = jwt.verify(req.body.token, process.env.JWT_SECRET); | ||
req.token = req.body.token; | ||
return next(); | ||
} | ||
], | ||
|
||
verifyToken: [ | ||
body('email') | ||
.isEmail() | ||
.withMessage('Please provide a valid email address') | ||
.normalizeEmail() | ||
.custom((value, { req }) => { | ||
const { email, password } = req.body; | ||
const user = users.find(user => (user.email === email)); | ||
if (user === undefined) { | ||
throw new Error('User not found'); | ||
} | ||
if (user.password !== password) { | ||
throw new Error('Wrong password'); | ||
} | ||
return value; | ||
}), | ||
|
||
body('password') | ||
.not().isEmpty().withMessage('Password is required') | ||
.isLength({ min: 8 }).trim() | ||
.withMessage('Password must be at least 8 characters') | ||
.isLength({ max: 16 }) | ||
.withMessage('Password must be at most 16 characters') | ||
.isAlphanumeric().withMessage('Password must be alphanumeric'), | ||
|
||
sanitizeBody('email').trim().escape(), | ||
sanitizeBody('password').trim().escape(), | ||
(req, res, next) => { | ||
|
||
const errors = validationResult(req); | ||
if (!errors.isEmpty()) { | ||
return res.status(422).json({ errors: errors.array() }); | ||
} | ||
|
||
try { | ||
req.user = jwt.verify(req.body.token, process.env.JWT_SECRET); | ||
req.token = req.body.token; | ||
return next(); | ||
} | ||
catch (err) { | ||
res.status(422).json({ error: 'Invalid token' }); | ||
} | ||
catch (err) { | ||
res.status(422).json({ error: 'Invalid token' }); | ||
} | ||
] | ||
} | ||
}; | ||
|
||
export default AuthenticationMiddleware; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { body, validationResult } from 'express-validator/check'; | ||
import { sanitizeBody } from 'express-validator/filter'; | ||
|
||
// import { dev_logger } from '../utils/loggers'; | ||
|
||
const ParamterValidators = { | ||
emailValidator: [ | ||
body('email') | ||
.isEmail() | ||
.withMessage('Please provide a valid email address') | ||
.normalizeEmail(), | ||
sanitizeBody('email').trim().escape(), | ||
|
||
(req, res, next) => { | ||
const errors = validationResult(req); | ||
if (!errors.isEmpty()) { | ||
return res.status(422).json({ errors: errors.array() }); | ||
} | ||
return next(); | ||
} | ||
], | ||
|
||
passwordValidator: [ | ||
body('password') | ||
.not().isEmpty().withMessage('Password is required') | ||
.isLength({ min: 8 }).trim() | ||
.withMessage('Password must be at least 8 characters') | ||
.isLength({ max: 16 }) | ||
.withMessage('Password must be at most 16 characters') | ||
.isAlphanumeric().withMessage('Password must be alphanumeric'), | ||
sanitizeBody('password').trim().escape(), | ||
sanitizeBody('confirm_password').trim().escape(), | ||
|
||
(req, res, next) => { | ||
const errors = validationResult(req); | ||
if (!errors.isEmpty()) { | ||
return res.status(422).json({ errors: errors.array() }); | ||
} | ||
return next(); | ||
} | ||
], | ||
|
||
confirmPasswordValidator: [ | ||
body('confirm_password') | ||
.custom((value, { req }) => { | ||
if (value !== req.body.password) { | ||
throw new Error( | ||
'Password confirmation does not match password' | ||
); | ||
} | ||
else return value; | ||
}), | ||
|
||
(req, res, next) => { | ||
const errors = validationResult(req); | ||
if (!errors.isEmpty()) { | ||
return res.status(422).json({ errors: errors.array() }); | ||
} | ||
return next(); | ||
} | ||
], | ||
|
||
updateProfileValidator: [ | ||
body('firstName') | ||
.not().isEmpty().withMessage('First name cannot be empty'), | ||
body('lastName') | ||
.not().isEmpty().withMessage('Last namecannot be empty'), | ||
body('phone') | ||
.not().isEmpty().withMessage('Phone number cannot be empty') | ||
.matches(/^0\d{10}$/).withMessage( | ||
'Wrong number format: E.G. 07012345678'), | ||
body('home') | ||
.not().isEmpty().withMessage('Home address cannot be empty'), | ||
body('office') | ||
.not().isEmpty().withMessage('Office address cannot be empty'), | ||
|
||
sanitizeBody('firstName').trim().escape(), | ||
sanitizeBody('lastName').trim().escape(), | ||
sanitizeBody('phone').trim().escape(), | ||
sanitizeBody('home').trim().escape(), | ||
sanitizeBody('office').trim().escape(), | ||
|
||
(req, res, next) => { | ||
const errors = validationResult(req); | ||
if (!errors.isEmpty()) { | ||
return res.status(422).json({ errors: errors.array() }); | ||
} | ||
return next(); | ||
} | ||
] | ||
}; | ||
|
||
export default ParamterValidators; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters