-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #246 from Gauravtb2253/middleware
closes : #236 added middleware
- Loading branch information
Showing
4 changed files
with
84 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |