Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Authentication checks for AGRO-rent services #917

Merged
merged 2 commits into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions backend/middleware/authMiddleware.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
const jwt = require('jsonwebtoken');
const jwt = require("jsonwebtoken");

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

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

req.user = decoded;
next();
});
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();
});
};

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

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', addToCart);
router.post('/addtoCart',authMiddleware, addToCart);

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

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

module.exports = router;
13 changes: 7 additions & 6 deletions backend/routes/rent/rentProductRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,26 @@
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', productController.createProduct);
router.post('/rent-products',authMiddleware, productController.createProduct);

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

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

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

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



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



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

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', wishlistController.addToWishlist);
router.post('/wishlist/add/:productId',authMiddleware, wishlistController.addToWishlist);

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

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

module.exports = router;
Loading