Skip to content

Commit

Permalink
Add: express-validator
Browse files Browse the repository at this point in the history
  • Loading branch information
Jun4928 committed Jan 6, 2021
1 parent 07e45a0 commit 3584345
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 13 deletions.
4 changes: 2 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ app.use(routes)

// general error handler
app.use((err, req, res, next) => {
const { status, message } = err
const { statusCode, message } = err
console.error(err)
res.status(status || 500).json({ message })
res.status(statusCode).json({ message })
})

module.exports = app
5 changes: 0 additions & 5 deletions controllers/UserController.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@ const { errorWrapper, errorGenerator } = require('../errors')

const signUp = errorWrapper(async (req, res) => {
const { email, password } = req.body
if (!email || !password) errorGenerator({ statusCode: 400, message: 'invalid input' })

const hashedPassword = await bcrypt.hash(password, 10)

const foundUser = await UserService.findUser({ email })

if (foundUser) errorGenerator({ statusCode: 409, message: 'duplicated' })

const createdUser = await UserService.createUser({
Expand All @@ -29,12 +26,10 @@ const logIn = errorWrapper(async (req, res) => {
const { email, password: inputPassword } = req.body

const foundUser = await UserService.findUser({ email })

if (!foundUser) errorGenerator({ statusCode: 400, message: 'client input invalid' })

const { id, password: hashedPassword } = foundUser
const isValidPassword = await bcrypt.compare(inputPassword, hashedPassword)

if (!isValidPassword) errorGenerator({ statusCode: 400, message: 'client input invalid' })

const token = jwt.sign({ id }, AUTH_TOKEN_SALT)
Expand Down
15 changes: 12 additions & 3 deletions errors/errorGenerator.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
const errorGenerator = ({ message, statusCode = 500 }) => {
const err = new Error(message)
err.status = statusCode
const DEFAULT_HTTP_STATUS_MESSAGES = {
400: 'Bad Requests',
401: 'Unauthorized',
403: 'Foribdden',
404: 'Not Found',
500: 'Internal Server Error',
503: 'Temporary Unavailable',
}

const errorGenerator = ({ message = '', statusCode = 500 }) => {
const err = new Error(message || DEFAULT_HTTP_STATUS_MESSAGES[statusCode])
err.statusCode = statusCode
throw err
}

Expand Down
7 changes: 7 additions & 0 deletions errors/errorWrapper.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
const { validationResult } = require('express-validator')
const errorGenerator = require('./errorGenerator')

const errorWrapper = (controller) => async (req, res, next) => {
try {
const errors = validationResult(req)
console.log(errors)
if (!errors.isEmpty()) errorGenerator({ statusCode: 400 })

await controller(req, res, next)
} catch (err) {
next(err)
Expand Down
1 change: 0 additions & 1 deletion middlewares/validateToken.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const validateToken = errorWrapper(async (req, res, next) => {
const { id } = jwt.verify(token, AUTH_TOKEN_SALT)

const foundUser = await UserService.findUser({ id })

if (!foundUser) errorGenerator({ statusCode: 404, message: 'user not found' })

req.foundUser = foundUser
Expand Down
19 changes: 19 additions & 0 deletions 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 package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"bcryptjs": "^2.4.3",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"express-validator": "^6.9.2",
"jsonwebtoken": "^8.5.1",
"morgan": "^1.10.0"
},
Expand Down
15 changes: 13 additions & 2 deletions routes/UserRouter.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
const express = require('express')
const router = express.Router()
const { body } = require('express-validator')

const { UserController } = require('../controllers')

router.post('/login', UserController.logIn)
router.post('/signup', UserController.signUp)
router.post(
'/login',
body('email').isEmail(),
body('password').isLength({ min: 5 }),
UserController.logIn
)
router.post(
'/signup',
body('email').isEmail(),
body('password').isLength({ min: 5 }),
UserController.signUp
)

module.exports = router

0 comments on commit 3584345

Please sign in to comment.