diff --git a/src/app.js b/src/app.js index f50bac8..849d084 100644 --- a/src/app.js +++ b/src/app.js @@ -3,16 +3,12 @@ const connectDB = require("../config/db"); const medicationRoutes = require("./routes/medicationRoutes.js"); require("dotenv").config(); - -const mainRouter = require('./routes/mainRouter.js'); -const testRoutes = require('./routes/testRoutes'); -const authRoutes = require('./routes/authRoutes'); +const authRoutes = require("./routes/authRoutes"); const app = express(); const cors = require("cors"); const favicon = require("express-favicon"); const logger = require("morgan"); - const mainRouter = require("./routes/mainRouter.js"); const testRoutes = require("./routes/testRoutes"); const errorHandler = require("./middleware/errorHandler.js"); @@ -29,8 +25,8 @@ app.use(express.static("public")); app.use(favicon(__dirname + "/public/favicon.ico")); // routes -app.use('/api/v1', mainRouter); -app.use('/api/v1/test', testRoutes); +app.use("/api/v1", mainRouter); +app.use("/api/v1/test", testRoutes); app.use("/api/v1/auth", authRoutes); app.use("/api/v1", mainRouter); app.use("/api/v1/test", testRoutes); diff --git a/src/controllers/authController.js b/src/controllers/authController.js index 12623c3..00a9f58 100644 --- a/src/controllers/authController.js +++ b/src/controllers/authController.js @@ -1,5 +1,5 @@ const User = require("../models/UserModel"); -const bcrypt = require("bcryptjs"); + const jwt = require("jsonwebtoken"); // Controller for Signup @@ -51,7 +51,7 @@ const login = async (req, res) => { const token = jwt.sign( { id: user._id, role: user.role }, process.env.JWT_SECRET, - { expiresIn: "1h" } + { expiresIn: "1h" }, ); res.status(200).json({ diff --git a/src/controllers/mainController.js b/src/controllers/mainController.js index e00e55c..f23c286 100644 --- a/src/controllers/mainController.js +++ b/src/controllers/mainController.js @@ -1,4 +1,3 @@ - const mainController = {}; mainController.get = (req, res) => { diff --git a/src/middleware/authMiddleware.js b/src/middleware/authMiddleware.js index 92a65ec..b2b2aa0 100644 --- a/src/middleware/authMiddleware.js +++ b/src/middleware/authMiddleware.js @@ -19,7 +19,7 @@ const authenticate = (req, res, next) => { // Attach the user data (from token) to the request object req.user = decoded; - next(); + next(); } catch (err) { return res.status(400).json({ success: false, diff --git a/src/middleware/roleMiddleware.js b/src/middleware/roleMiddleware.js index 10a0ef1..dc722e3 100644 --- a/src/middleware/roleMiddleware.js +++ b/src/middleware/roleMiddleware.js @@ -1,6 +1,6 @@ const roleMiddleware = (requiredRoles) => { return (req, res, next) => { - const userRole = req.user?.role; + const userRole = req.user?.role; if (!requiredRoles.includes(userRole)) { return res.status(403).json({ diff --git a/src/models/UserModel.js b/src/models/UserModel.js index a53b2b0..ef8da93 100644 --- a/src/models/UserModel.js +++ b/src/models/UserModel.js @@ -38,7 +38,7 @@ const userSchema = new mongoose.Schema( }, { timestamps: true, - } + }, ); // Hash password before saving the user diff --git a/src/routes/authRoutes.js b/src/routes/authRoutes.js index d8e510b..5c40f37 100644 --- a/src/routes/authRoutes.js +++ b/src/routes/authRoutes.js @@ -14,7 +14,7 @@ router.get( roleMiddleware(["admin"]), (req, res) => { res.status(200).json({ message: "Welcome, Admin!" }); - } + }, ); router.get( @@ -23,7 +23,7 @@ router.get( roleMiddleware(["admin", "inventoryManager"]), // Admins and inventory managers can access (req, res) => { res.status(200).json({ message: "Welcome, Inventory Manager!" }); - } + }, ); router.get( @@ -32,7 +32,7 @@ router.get( roleMiddleware(["admin", "inventoryManager", "clerk"]), // Allow clerk access (req, res) => { res.status(200).json({ message: "Welcome, Clerk!" }); - } + }, ); module.exports = router; diff --git a/src/routes/testRoutes.js b/src/routes/testRoutes.js index fd0476e..1541d3a 100644 --- a/src/routes/testRoutes.js +++ b/src/routes/testRoutes.js @@ -52,7 +52,7 @@ router.post("/test-create-user", async (req, res, next) => { router.get("/test-get-user", async (req, res, next) => { try { const user = await User.findOne({ email: req.query.email }).select( - "+password" + "+password", ); if (!user) { return res @@ -184,9 +184,7 @@ router.post("/test-reset-password", async (req, res, next) => { const isMatch = await user.comparePassword(currentPassword); if (!isMatch) { - return res - .status(401) - .json({ message: "Current password is incorrect" }); + return res.status(401).json({ message: "Current password is incorrect" }); } user.password = newPassword;