added isValidLogin Body request

This commit is contained in:
Kfir Dayan 2023-06-24 19:38:22 +03:00
parent e5ad4fc0c1
commit b5ea0a69a5
2 changed files with 18 additions and 6 deletions

View file

@ -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
}

View file

@ -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)