removed Create user validation from login + done middleware of product creation
This commit is contained in:
parent
23f030ed56
commit
e5ad4fc0c1
6 changed files with 46 additions and 20 deletions
14
src/middlewares/productResourceValidation.ts
Normal file
14
src/middlewares/productResourceValidation.ts
Normal file
|
@ -0,0 +1,14 @@
|
|||
import { Request, Response, NextFunction } from 'express';
|
||||
import { ApiError } from '../utils/ApiError';
|
||||
|
||||
const isValidBody = async (req: Request, res: Response, next: NextFunction) => {
|
||||
const { name, description, price } = req.body;
|
||||
if (!name || !description || !price) {
|
||||
console.log("Error in product validation")
|
||||
const error = new ApiError(`${!name ? 'name' : !description ? 'description' : 'price'} is required}`);
|
||||
error.statusCode = 400;
|
||||
error.status = 'fail';
|
||||
return next(error);
|
||||
}
|
||||
next();
|
||||
}
|
28
src/middlewares/usersResourceValidation.ts
Normal file
28
src/middlewares/usersResourceValidation.ts
Normal file
|
@ -0,0 +1,28 @@
|
|||
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 user = req.body;
|
||||
if (!user.email || !user.password || !user.address) {
|
||||
const error = new ApiError(`${!user.email ? 'email' : !user.password ? 'password' : 'address'} is required}`);
|
||||
error.statusCode = 400;
|
||||
error.status = 'fail';
|
||||
return next(error);
|
||||
}
|
||||
|
||||
const { valid, reason } = await validate(user.email);
|
||||
if (!valid) {
|
||||
const error = new ApiError(`Invalid email: ${reason}`);
|
||||
error.statusCode = 400;
|
||||
error.status = 'fail';
|
||||
return next(error);
|
||||
}
|
||||
|
||||
next();
|
||||
}
|
||||
|
||||
export {
|
||||
isValidBody
|
||||
}
|
|
@ -1,25 +1,9 @@
|
|||
import { User } from "../schemas/userSchema";
|
||||
import validate from 'deep-email-validator';
|
||||
import { ApiError } from "../utils/ApiError";
|
||||
import bcrypt from 'bcryptjs';
|
||||
|
||||
const createUser = async (user: any) => {
|
||||
|
||||
if (!user.email || !user.password || !user.address) {
|
||||
const error = new ApiError('Missing required fields');
|
||||
error.statusCode = 400;
|
||||
error.status = 'fail';
|
||||
return error;
|
||||
}
|
||||
|
||||
const { valid, reason, validators } = await validate(user.email);
|
||||
if (!valid) {
|
||||
const error = new ApiError(reason);
|
||||
error.statusCode = 400;
|
||||
error.status = 'fail';
|
||||
return error;
|
||||
}
|
||||
|
||||
const userExists = await User.exists({ email: user.email });
|
||||
if (userExists) {
|
||||
const error = new ApiError('User already exists, Try login :)');
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import express from 'express';
|
||||
import { authenticateToken } from '../middlewares/checkAuth';
|
||||
import { create, getAll, getProduct } from '../controllers/productController';
|
||||
import { create, getAll, getProduct } from '../controllers/ProductController';
|
||||
|
||||
const productRouter = express.Router();
|
||||
|
||||
|
|
|
@ -1,9 +1,10 @@
|
|||
import express from 'express';
|
||||
import { create, login, logout, getAll, deleteHandler } from '../controllers/userController';
|
||||
import { create, login, logout, getAll, deleteHandler } from '../controllers/UserController';
|
||||
import { isValidBody } from '../middlewares/usersResourceValidation';
|
||||
|
||||
const userRouter = express.Router();
|
||||
|
||||
userRouter.post('/', create);
|
||||
userRouter.post('/', isValidBody, create);
|
||||
userRouter.get('/', getAll);
|
||||
userRouter.post('/login', login);
|
||||
userRouter.post('/logout', logout);
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
import mongoose, { Schema, Document } from 'mongoose';
|
||||
import { Request } from 'express';
|
||||
|
||||
interface IProduct extends Document {
|
||||
name: string;
|
||||
|
|
Loading…
Reference in a new issue