From ca14c2ec4fa8bc4c9efa107883347edddcdfa802 Mon Sep 17 00:00:00 2001 From: Kfir Dayan Date: Sat, 10 Jun 2023 17:02:55 +0300 Subject: [PATCH] small stuff --- src/controllers/CartController.ts | 2 +- src/controllers/ProductController.ts | 6 ++---- src/controllers/UserController.ts | 11 +++++++++-- src/middlewares/checkAuth.ts | 2 +- src/routes/product.ts | 2 -- 5 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/controllers/CartController.ts b/src/controllers/CartController.ts index d3f2184..972a9c8 100644 --- a/src/controllers/CartController.ts +++ b/src/controllers/CartController.ts @@ -55,7 +55,7 @@ export async function checkout(req: Request, res: Response) { const { userId } = req.body; const usersCart = await Cart.findOne({ userId }); - console.log(usersCart) + if (!usersCart) { res.status(404).json({ error: 'Cart not found.' }); return; diff --git a/src/controllers/ProductController.ts b/src/controllers/ProductController.ts index 59e8261..7b0d35a 100644 --- a/src/controllers/ProductController.ts +++ b/src/controllers/ProductController.ts @@ -25,10 +25,8 @@ export async function createProduct(req: Request, res: Response) { export async function listProducts(req: Request, res: Response) { try { const { page, limit } = req.query; - const products = await Product.find() - .sort({ price: 1 }) - .skip(Number(page) * Number(limit)) - .limit(Number(limit)); + const dbLimit = Number(limit) || 50; + const products = await Product.find().sort({ price: 1 }).skip(Number(page) * Number(dbLimit)).limit(Number(dbLimit)); res.json(products); } catch (error) { diff --git a/src/controllers/UserController.ts b/src/controllers/UserController.ts index 35c0ec9..f6cd72c 100644 --- a/src/controllers/UserController.ts +++ b/src/controllers/UserController.ts @@ -11,6 +11,11 @@ export async function createUser(req: Request, res: Response) { if (!(email && password && firstName && lastName && address)) { return res.status(400).json({ error: 'All inputs are required' }); } + // checkIfUserExists return true if the user exists + const userExists = await User.exists({ email }); + if(userExists) { + return res.status(400).json({ error: 'User already exists' }); + } const hashedPassword = await bcrypt.hash(password, 10); const user: IUser = await User.create({ @@ -21,7 +26,9 @@ export async function createUser(req: Request, res: Response) { address, }); - res.json(user); + res.status(200).json({ + massage: 'User created successfully' + }); } catch (error) { console.error('Error creating user:', error); res.status(500).json({ error: 'An error occurred while creating the user.' }); @@ -54,7 +61,7 @@ export async function login(req: Request, res: Response) { // Send the JWT as the response res.status(200).json({ - username: user.firstName + token }); } catch (error) { console.error('Error during login:', error); diff --git a/src/middlewares/checkAuth.ts b/src/middlewares/checkAuth.ts index b0a0b5c..959c49f 100644 --- a/src/middlewares/checkAuth.ts +++ b/src/middlewares/checkAuth.ts @@ -14,7 +14,7 @@ export function authenticateToken(req: AuthenticatedRequest, res: Response, next return res.status(401).json({ error: 'Unauthorized' }); } - const user_id = jwt.verify(token, process.env.JWT_SECRET as string, (err, decoded) => { + jwt.verify(token, process.env.JWT_SECRET as string, (err: any, decoded: { userId: any; }) => { if (err) { return res.status(401).json({ error: 'In Valid Token' }); } diff --git a/src/routes/product.ts b/src/routes/product.ts index 8112159..5225b6b 100644 --- a/src/routes/product.ts +++ b/src/routes/product.ts @@ -2,8 +2,6 @@ import express, { Request } from 'express'; import { authenticateToken } from '../middlewares/checkAuth'; import { createProduct, listProducts } from '../controllers/ProductController'; - - const productRouter = express.Router(); productRouter.post('/', authenticateToken, createProduct)