diff --git a/src/middlewares/usersResourceValidation.ts b/src/middlewares/usersResourceValidation.ts index f1d444c..7282a4f 100644 --- a/src/middlewares/usersResourceValidation.ts +++ b/src/middlewares/usersResourceValidation.ts @@ -2,7 +2,19 @@ import { Request, Response, NextFunction } from 'express'; import { ApiError } from '../utils/ApiError'; import validate from 'deep-email-validator'; -const isValidBody = async (req: Request, res: Response, next: NextFunction) => { +const isValidLogin = async (req: Request, res: Response, next: NextFunction) => { + + const user = req.body; + if (!user.email || !user.password) { + const error = new ApiError(`${!user.email ? 'email' : 'password'} is required`); + error.statusCode = 400; + error.status = 'fail'; + return next(error); + } + next(); +} + +const isValidCreateUser = async (req: Request, res: Response, next: NextFunction) => { const user = req.body; if (!user.email || !user.password || !user.address) { @@ -19,10 +31,10 @@ const isValidBody = async (req: Request, res: Response, next: NextFunction) => { error.status = 'fail'; return next(error); } - next(); } export { - isValidBody + isValidLogin, + isValidCreateUser } \ No newline at end of file diff --git a/src/routes/userRouter.ts b/src/routes/userRouter.ts index 46a08fe..77ae7e2 100644 --- a/src/routes/userRouter.ts +++ b/src/routes/userRouter.ts @@ -1,12 +1,12 @@ import express from 'express'; import { create, login, logout, getAll, deleteHandler } from '../controllers/UserController'; -import { isValidBody } from '../middlewares/usersResourceValidation'; +import { isValidCreateUser, isValidLogin } from '../middlewares/usersResourceValidation'; const userRouter = express.Router(); -userRouter.post('/', isValidBody, create); +userRouter.post('/', isValidCreateUser, create); userRouter.get('/', getAll); -userRouter.post('/login', login); +userRouter.post('/login', isValidLogin, login); userRouter.post('/logout', logout); userRouter.delete('/:id', deleteHandler)