From 92b65e02d0eff8541cdab42ff2afa6d09e1da3d4 Mon Sep 17 00:00:00 2001 From: Kfir Dayan Date: Thu, 8 Jun 2023 20:08:15 +0300 Subject: [PATCH] fixing auth apply foreach route --- src/controllers/UserController.ts | 4 ---- src/index.ts | 9 +++++++-- src/middlewares/authMiddleware.ts | 10 +++++++--- src/models/User.ts | 1 + src/routes/product.ts | 9 +++++++++ src/routes/user.ts | 8 ++++---- 6 files changed, 28 insertions(+), 13 deletions(-) create mode 100644 src/routes/product.ts diff --git a/src/controllers/UserController.ts b/src/controllers/UserController.ts index 43a2995..60bb878 100644 --- a/src/controllers/UserController.ts +++ b/src/controllers/UserController.ts @@ -6,11 +6,7 @@ import { User, IUser } from '../models/User'; export async function createUser(req: Request, res: Response) { try { const { firstName, lastName, email, password, address } = req.body; - - // Hash the password const hashedPassword = await bcrypt.hash(password, 10); - - // Create the user in the database const user: IUser = await User.create({ firstName, lastName, diff --git a/src/index.ts b/src/index.ts index 32f03fa..0766796 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,10 @@ import express from 'express'; import mongoose from 'mongoose'; + +import userRouter from './routes/user'; +import ProductrRouter from './routes/product'; + import { authenticateToken } from './middlewares/authMiddleware'; -import userRoutes from './routes/user'; const env = require('dotenv').config().parsed; @@ -11,6 +14,7 @@ const PORT = 3000; app.use(express.json()); app.use(authenticateToken); + // Connect to MongoDB using Mongoose mongoose.connect(env.DATABASE_URL); @@ -23,7 +27,8 @@ db.once('open', () => { }); // Routes -app.use('/users', userRoutes); +app.use('/users', userRouter); +app.use('/products', ProductrRouter); // Start server app.listen(PORT, () => { diff --git a/src/middlewares/authMiddleware.ts b/src/middlewares/authMiddleware.ts index 8ee1156..dfa7e41 100644 --- a/src/middlewares/authMiddleware.ts +++ b/src/middlewares/authMiddleware.ts @@ -5,18 +5,22 @@ interface AuthenticatedRequest extends Request { user?: JwtPayload | string; } -const exceptionRoutes: string[] = ['/login', '/']; +const exceptionRoutes: string[] = ['/users/login', '/users']; // Middleware function to authenticate requests export function authenticateToken(req: AuthenticatedRequest, res: Response, next: NextFunction) { const authHeader = req.headers['authorization']; const token = authHeader && authHeader.split(' ')[1]; - if (!token && exceptionRoutes.includes(req.originalUrl)) { + if(token) { + console.log('token', token); + } + + if (!token && exceptionRoutes.includes(req.route)) { return res.sendStatus(401); // Unauthorized } - if (!exceptionRoutes.includes(req.originalUrl)) { + if (!exceptionRoutes.includes(req.route)) { // Skip authentication for exception routes return next(); } diff --git a/src/models/User.ts b/src/models/User.ts index 022a3c5..7223b16 100644 --- a/src/models/User.ts +++ b/src/models/User.ts @@ -14,6 +14,7 @@ export interface IProduct extends Document { name: string; description: string; price: number; + userId: string; createdAt: Date; updatedAt: Date; } diff --git a/src/routes/product.ts b/src/routes/product.ts new file mode 100644 index 0000000..2074475 --- /dev/null +++ b/src/routes/product.ts @@ -0,0 +1,9 @@ +import express from 'express'; + +const ProductrRouter = express.Router(); + +ProductrRouter.post('/', (req, res) => { + res.send('Create product'); +}) + +export default ProductrRouter; \ No newline at end of file diff --git a/src/routes/user.ts b/src/routes/user.ts index aa8797d..de21658 100644 --- a/src/routes/user.ts +++ b/src/routes/user.ts @@ -1,9 +1,9 @@ import express from 'express'; import { createUser, login } from '../controllers/UserController'; -const router = express.Router(); +const userRouter = express.Router(); -router.post('/', createUser); -router.post('/login', login) +userRouter.post('/', createUser); +userRouter.post('/login', login); -export default router; \ No newline at end of file +export default userRouter; \ No newline at end of file