Compare commits
4 commits
23f030ed56
...
12dc986941
Author | SHA1 | Date | |
---|---|---|---|
12dc986941 | |||
35ca5e7e45 | |||
b5ea0a69a5 | |||
e5ad4fc0c1 |
6 changed files with 62 additions and 22 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();
|
||||||
|
}
|
41
src/middlewares/usersResourceValidation.ts
Normal file
41
src/middlewares/usersResourceValidation.ts
Normal 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
|
||||||
|
}
|
|
@ -1,25 +1,9 @@
|
||||||
import { User } from "../schemas/userSchema";
|
import { User } from "../schemas/userSchema";
|
||||||
import validate from 'deep-email-validator';
|
|
||||||
import { ApiError } from "../utils/ApiError";
|
import { ApiError } from "../utils/ApiError";
|
||||||
import bcrypt from 'bcryptjs';
|
import bcrypt from 'bcryptjs';
|
||||||
|
|
||||||
const createUser = async (user: any) => {
|
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 });
|
const userExists = await User.exists({ email: user.email });
|
||||||
if (userExists) {
|
if (userExists) {
|
||||||
const error = new ApiError('User already exists, Try login :)');
|
const error = new ApiError('User already exists, Try login :)');
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import express from 'express';
|
import express from 'express';
|
||||||
import { authenticateToken } from '../middlewares/checkAuth';
|
import { authenticateToken } from '../middlewares/checkAuth';
|
||||||
import { create, getAll, getProduct } from '../controllers/productController';
|
import { create, getAll, getProduct } from '../controllers/ProductController';
|
||||||
|
|
||||||
const productRouter = express.Router();
|
const productRouter = express.Router();
|
||||||
|
|
||||||
|
|
|
@ -1,12 +1,14 @@
|
||||||
import express from 'express';
|
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();
|
const userRouter = express.Router();
|
||||||
|
|
||||||
userRouter.post('/', create);
|
userRouter.post('/', isValidCreateUser, create);
|
||||||
userRouter.get('/', getAll);
|
userRouter.get('/', getAll);
|
||||||
userRouter.post('/login', login);
|
userRouter.post('/login', isValidLogin, login);
|
||||||
userRouter.post('/logout', logout);
|
userRouter.post('/logout', logout);
|
||||||
userRouter.delete('/:id', deleteHandler)
|
userRouter.delete('/:id', authenticateToken ,deleteHandler)
|
||||||
|
|
||||||
export default userRouter;
|
export default userRouter;
|
|
@ -1,5 +1,4 @@
|
||||||
import mongoose, { Schema, Document } from 'mongoose';
|
import mongoose, { Schema, Document } from 'mongoose';
|
||||||
import { Request } from 'express';
|
|
||||||
|
|
||||||
interface IProduct extends Document {
|
interface IProduct extends Document {
|
||||||
name: string;
|
name: string;
|
||||||
|
|
Loading…
Reference in a new issue