Skip to content

Commit 6f7737b

Browse files
committed
Add Authentication Check to Ensure only Matched Users can Enter a Room
Also, deleted obsolete route and controller method in historyCotnroller in question service
1 parent 4d6f6ec commit 6f7737b

File tree

3 files changed

+9
-27
lines changed

3 files changed

+9
-27
lines changed

backend/matching-service/controllers/roomController.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import { AuthenticatedRequest } from "middlewares/auth";
12
import roomModel from "../models/RoomSchema";
2-
import { Request, Response, NextFunction } from "express";
3+
import { Response, NextFunction } from "express";
34

45
const ROOM_LIFESPAN = parseInt(process.env.ROOM_LIFESPAN || "86400000"); // 86400000ms = 1 day
56

67
export async function getRoomDetails(
7-
request: Request,
8+
request: AuthenticatedRequest,
89
response: Response,
910
next: NextFunction
1011
) {
@@ -13,11 +14,14 @@ export async function getRoomDetails(
1314
console.log(roomId)
1415
const room = await roomModel.findOne({ roomId });
1516
if (!room) {
16-
throw new Error("Room not found");
17+
throw new Error("Room not found.");
18+
}
19+
if (room.participants.every((participant) => participant !== request.userId)) {
20+
throw new Error("Non-matched user cannot enter this room.");
1721
}
1822
if (Date.now() - room.createdAt.getTime() > ROOM_LIFESPAN) {
19-
throw new Error("Room has expired");
20-
}
23+
throw new Error("Room has expired.");
24+
}
2125
response.status(200).json({
2226
roomId,
2327
attemptStartedAt: room.createdAt.getTime(),

backend/question-service/controllers/historyController.ts

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -85,26 +85,6 @@ export const createOrUpdateUserHistoryEntry = async (req: any, res: Response) =>
8585
}
8686
};
8787

88-
export const removeRoomIdPresence = async (req: any, res: Response) => {
89-
try {
90-
const userId = req.userId;
91-
const { roomId } = req.params;
92-
93-
const existingEntries = await historyEntryModel.find({ roomId });
94-
const updatedEntries: string[] = [];
95-
96-
existingEntries.forEach(async (entry) => {
97-
entry.roomId = "";
98-
await entry.save();
99-
updatedEntries.push(entry._id.toString());
100-
});
101-
102-
return res.status(200).json({ updatedEntries });
103-
} catch (error) {
104-
return res.status(500).json({ error: getErrorMessage(error) });
105-
}
106-
};
107-
10888
export const deleteUserHistoryEntry = async (req: any, res: Response) => {
10989
try {
11090
const userId = req.userId;

backend/question-service/routes/historyRoutes.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@ import {
55
deleteUserHistoryEntry,
66
deleteUserHistoryEntries,
77
deleteAllUserHistoryEntries,
8-
removeRoomIdPresence,
98
} from "../controllers/historyController";
109
import { verifyAccessToken } from "../middlewares/basic-access-control";
1110

1211
const router = express.Router();
1312

1413
router.get("/", verifyAccessToken, getUserHistoryEntries);
1514
router.post("/", verifyAccessToken, createOrUpdateUserHistoryEntry);
16-
router.post("/room/:id", verifyAccessToken, removeRoomIdPresence);
1715
router.delete("/user/:id", verifyAccessToken, deleteUserHistoryEntry);
1816
router.delete("/user", verifyAccessToken, deleteUserHistoryEntries);
1917
router.delete("/all", verifyAccessToken, deleteAllUserHistoryEntries);

0 commit comments

Comments
 (0)