Compare commits

..

No commits in common. "12dc98694184815d1afefb028a32376213f459c3" and "23f030ed56f3e035cc1cc43418f779a0d387e53d" have entirely different histories.

6 changed files with 22 additions and 62 deletions

View file

@ -1,14 +0,0 @@
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

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

View file

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