Skip to content

Commit

Permalink
Merge pull request #246 from Gauravtb2253/middleware
Browse files Browse the repository at this point in the history
closes : #236 
added middleware
  • Loading branch information
dhairyagothi authored Oct 15, 2024
2 parents 6b1e00a + 5417508 commit 07b5bce
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 17 deletions.
32 changes: 32 additions & 0 deletions backend/middleware/auth.middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { ApiError } from "../utils/ApiError.js";
import asyncHandler from "../utils/asyncHandler.js";
import jwt from "jsonwebtoken";
import User from "../models/User.js";

export const verifyJWT = asyncHandler(async (req, _, next) => {
try {
const token =
req.cookies?.accessToken ||
req.header("Authorization")?.replace("Bearer ", "");

// console.log(token);
if (!token) {
throw new ApiError(401, "Unauthorized request");
}

const decodedToken = jwt.verify(token, process.env.JWT_SECRET);

const user = await User.findById(decodedToken?._id).select(
"-password "
);

if (!user) {
throw new ApiError(401, "Invalid Access Token");
}

req.user = user;
next();
} catch (error) {
throw new ApiError(401, error?.message || "Invalid access token");
}
});
38 changes: 21 additions & 17 deletions backend/routes/authRoutes.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,40 @@
import express from 'express';
import { registerUser, loginUser, logoutUser, verifyUser } from '../controllers/authController.js';
import { createCloakroomBooking } from '../controllers/cloakroomController.js';
import { createWheelchairBooking } from '../controllers/WheelchairController.js';
import { createCoolieBooking } from '../controllers/coolieController.js';
import { sendStations } from '../controllers/stationsController.js';
import express from "express";
import {
registerUser,
loginUser,
logoutUser,
verifyUser,
} from "../controllers/authController.js";
import { createCloakroomBooking } from "../controllers/cloakroomController.js";
import { createWheelchairBooking } from "../controllers/WheelchairController.js";
import { createCoolieBooking } from "../controllers/coolieController.js";
import { sendStations } from "../controllers/stationsController.js";
import { verifyJWT } from "../middleware/auth.middleware.js";

const router = express.Router();

// Register route
router.post('/register', registerUser);
router.post("/register", registerUser);

// Login route
router.post('/login', loginUser);
router.post("/login", loginUser);

// Logout route
router.post('/logout', logoutUser);
router.post("/logout", verifyJWT, logoutUser);

// Verify route
router.get('/verify', verifyUser);
router.get("/verify", verifyUser);

//cloakroom bookings route
router.post('/bookCloakroom', createCloakroomBooking);
router.post("/bookCloakroom", verifyJWT, createCloakroomBooking);

//wheelchair bookings route
router.post('/bookWheelchair', createWheelchairBooking);
router.post("/bookWheelchair", verifyJWT, createWheelchairBooking);

//coolie bookings route
router.post('/bookCoolie', createCoolieBooking);
router.post("/bookCoolie", verifyJWT, createCoolieBooking);

// get all stations route
router.get("/all-stations",sendStations)
router.get("/all-stations", verifyJWT, sendStations);



export default router;
export default router;
24 changes: 24 additions & 0 deletions backend/utils/ApiError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class ApiError extends Error {
constructor(
statusCode,
message = "Something went wrong",
errors = [],
stack = ""
) {
super(message);
this.statusCode = statusCode;
this.data = null;
this.message = message;
this.success = false;
this.errors = errors;

if (stack) {
this.stack = stack;
} else {
Error.captureStackTrace(this, this.constructor);
}
}
}

export { ApiError };

7 changes: 7 additions & 0 deletions backend/utils/asyncHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const asyncHandler = (requestHandler) => {
return (req, res, next) => {
Promise.resolve(requestHandler(req, res, next)).catch((err) => next(err));
};
};

export default asyncHandler;

0 comments on commit 07b5bce

Please sign in to comment.