added isValidLogin Body request
This commit is contained in:
parent
e5ad4fc0c1
commit
b5ea0a69a5
2 changed files with 18 additions and 6 deletions
|
@ -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
|
||||
}
|
|
@ -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)
|
||||
|
||||
|
|
Loading…
Reference in a new issue