Skip to content

Commit

Permalink
Revert "Add Authentication checks for AGRO-rent services"
Browse files Browse the repository at this point in the history
  • Loading branch information
manikumarreddyu authored Nov 10, 2024
1 parent ab919f5 commit 9db5507
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 39 deletions.
32 changes: 8 additions & 24 deletions backend/middleware/authMiddleware.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,15 @@
const jwt = require("jsonwebtoken");
const jwt = require('jsonwebtoken');

const authMiddleware = (req, res, next) => {
const token = req.headers.authorization?.split(" ")[1];
const token = req.headers.authorization?.split(' ')[1];
if (!token) return res.status(403).json({ message: "No token provided" });

if (!token) {
console.error("Authorization token missing from request headers");
return res.status(403).json({ message: "No token provided" });
}
jwt.verify(token, process.env.JWT_SECRET, (err, decoded) => {
if (err) return res.status(401).json({ message: "Unauthorized" });

jwt.verify(token, process.env.JWT_SECRET, (err, decoded) => {
if (err) {
console.error(`JWT verification failed: ${err.message}`, { token });

if (err.name === "TokenExpiredError") {
return res.status(401).json({ message: "Token has expired" });
}
if (err.name === "JsonWebTokenError") {
return res.status(401).json({ message: "Invalid token" });
}

return res.status(401).json({ message: "Unauthorized" });
}

req.user = decoded;

next();
});
req.user = decoded;
next();
});
};

module.exports = authMiddleware;
7 changes: 3 additions & 4 deletions backend/routes/rent/rentCartRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@

const express = require('express');
const { addToCart, viewCart, removeFromCart } = require('../../controllers/rent/RentCartController');
const authMiddleware = require('../../middleware/authMiddleware');

const router = express.Router();

// Add to cart
router.post('/addtoCart',authMiddleware, addToCart);
router.post('/addtoCart', addToCart);

// View cart
router.get('/getCart/:userId',authMiddleware, viewCart);
router.get('/getCart/:userId', viewCart);

// Remove from cart
router.delete('/remove/:productId',authMiddleware, removeFromCart);
router.delete('/remove/:productId', removeFromCart);

module.exports = router;
13 changes: 6 additions & 7 deletions backend/routes/rent/rentProductRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,25 @@
const express = require('express');
const router = express.Router();
const productController = require('../../controllers/rent/RentProductController');
const authMiddleware = require('../../middleware/authMiddleware');

// Create a new product
router.post('/rent-products',authMiddleware, productController.createProduct);
router.post('/rent-products', productController.createProduct);

// Get all products
router.get('/rent-products',authMiddleware, productController.getAllProducts);
router.get('/rent-products', productController.getAllProducts);

// Get a single product by ID
router.get('/rent-products/:id',authMiddleware, productController.getProductById);
router.get('/rent-products/:id', productController.getProductById);

// Update a product by ID
router.put('/rent-products/:id',authMiddleware, productController.updateProduct);
router.put('/rent-products/:id', productController.updateProduct);

// Delete a product by ID
router.delete('/rent-products/:id',authMiddleware, productController.deleteProduct);
router.delete('/rent-products/:id', productController.deleteProduct);



router.get('/filtered-rent-products',authMiddleware, productController.getFilteredProducts );
router.get('/filtered-rent-products', productController.getFilteredProducts );



Expand Down
7 changes: 3 additions & 4 deletions backend/routes/rent/rentWishlistRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@

const express = require('express');
const wishlistController = require('../../controllers/rent/RentWishlistController');
const authMiddleware = require('../../middleware/authMiddleware');


const router = express.Router();

// Add to Wishlist
router.post('/wishlist/add/:productId',authMiddleware, wishlistController.addToWishlist);
router.post('/wishlist/add/:productId', wishlistController.addToWishlist);

// Remove from Wishlist
router.delete('/wishlist/remove/:productId',authMiddleware, wishlistController.removeFromWishlist);
router.delete('/wishlist/remove/:productId', wishlistController.removeFromWishlist);

// Get Wishlist
router.get('/wishlist',authMiddleware, wishlistController.getWishlist);
router.get('/wishlist',wishlistController.getWishlist);

module.exports = router;

0 comments on commit 9db5507

Please sign in to comment.