Compare commits

..

4 commits

6 changed files with 62 additions and 22 deletions

View 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();
}

View file

@ -0,0 +1,41 @@
import { Request, Response, NextFunction } from 'express';
import { ApiError } from '../utils/ApiError';
import validate from 'deep-email-validator';
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) {
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 {
isValidLogin,
isValidCreateUser
}

View file

@ -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 :)');

View file

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

View file

@ -1,12 +1,14 @@
import express from 'express';
import { create, login, logout, getAll, deleteHandler } from '../controllers/userController';
import { create, login, logout, getAll, deleteHandler } from '../controllers/UserController';
import { isValidCreateUser, isValidLogin } from '../middlewares/usersResourceValidation';
import { authenticateToken } from '../middlewares/checkAuth';
const userRouter = express.Router();
userRouter.post('/', 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)
userRouter.delete('/:id', authenticateToken ,deleteHandler)
export default userRouter;

View file

@ -1,5 +1,4 @@
import mongoose, { Schema, Document } from 'mongoose';
import { Request } from 'express';
interface IProduct extends Document {
name: string;