Skip to content

Commit 44e4390

Browse files
authored
Merge pull request #102 from njxue/history-403
Use verifyAccessToken instead of authenticateToken
2 parents d846c8c + bdad8b0 commit 44e4390

File tree

5 files changed

+37
-93
lines changed

5 files changed

+37
-93
lines changed

backend/.prettierrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "printWidth": 120 }

backend/question-service/controllers/historyController.ts

Lines changed: 26 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,20 @@
1-
import { Response } from 'express';
2-
import historyEntryModel from '../models/HistoryEntry';
3-
import { AuthenticatedRequest } from 'middlewares/auth';
1+
import { Response, Request } from "express";
2+
import historyEntryModel from "../models/HistoryEntry";
43

54
const getErrorMessage = (error: unknown): string => {
65
if (error instanceof Error) return error.message;
7-
return 'An unexpected error occurred';
6+
return "An unexpected error occurred";
87
};
98

10-
const extractUserIdFromToken = (req: AuthenticatedRequest): string | null => {
11-
const userId = req.userId;
12-
if (!userId) {
13-
console.error('userId missing - Token is likely invalid');
14-
return null;
15-
}
16-
return userId
17-
};
18-
19-
export const getUserHistoryEntries = async (req: AuthenticatedRequest, res: Response) => {
9+
export const getUserHistoryEntries = async (req: any, res: Response) => {
2010
try {
21-
const userId = extractUserIdFromToken(req);
22-
23-
if (!userId) {
24-
return res.status(401).json({ error: 'Invalid or missing token' });
25-
}
11+
const userId = req.userId;
2612

27-
const historyEntries = await historyEntryModel.find({ userId })
28-
.populate({
29-
path: 'question',
13+
const historyEntries = await historyEntryModel.find({ userId }).populate({
14+
path: "question",
3015
populate: {
31-
path: 'categories',
32-
model: 'category',
16+
path: "categories",
17+
model: "category",
3318
},
3419
});
3520
const historyViewModels = historyEntries.map((entry) => {
@@ -43,25 +28,22 @@ export const getUserHistoryEntries = async (req: AuthenticatedRequest, res: Resp
4328
difficulty: entry.question.difficulty,
4429
topics: entry.question.categories.map((cat: any) => cat.name),
4530
attemptCodes: entry.attemptCodes,
46-
}});
31+
};
32+
});
4733
res.status(200).json(historyViewModels);
4834
} catch (error) {
4935
res.status(500).json({ error: getErrorMessage(error) });
5036
}
5137
};
5238

53-
export const createOrUpdateUserHistoryEntry = async (req: AuthenticatedRequest, res: Response) => {
39+
export const createOrUpdateUserHistoryEntry = async (req: any, res: Response) => {
5440
try {
55-
const userId = extractUserIdFromToken(req);
56-
57-
if (!userId) {
58-
return res.status(401).json({ error: 'Invalid or missing token' });
59-
}
41+
const userId = req.userId;
6042

6143
const { questionId, roomId, attemptStartedAt, attemptCompletedAt, collaboratorId, attemptCode } = req.body;
6244

6345
if (!roomId) {
64-
return res.status(400).json({ error: 'roomId is required' });
46+
return res.status(400).json({ error: "roomId is required" });
6547
}
6648

6749
const existingEntry = await historyEntryModel.findOne({ userId, roomId });
@@ -96,13 +78,9 @@ export const createOrUpdateUserHistoryEntry = async (req: AuthenticatedRequest,
9678
}
9779
};
9880

99-
export const removeRoomIdPresence = async (req: AuthenticatedRequest, res: Response) => {
81+
export const removeRoomIdPresence = async (req: any, res: Response) => {
10082
try {
101-
const userId = extractUserIdFromToken(req);
102-
103-
if (!userId) {
104-
return res.status(401).json({ error: 'Invalid or missing token' });
105-
}
83+
const userId = req.userId;
10684
const { roomId } = req.params;
10785

10886
const existingEntries = await historyEntryModel.find({ roomId });
@@ -114,41 +92,32 @@ export const removeRoomIdPresence = async (req: AuthenticatedRequest, res: Respo
11492
updatedEntries.push(entry._id.toString());
11593
});
11694

117-
return res.status(200).json({ updatedEntries })
95+
return res.status(200).json({ updatedEntries });
11896
} catch (error) {
11997
return res.status(500).json({ error: getErrorMessage(error) });
12098
}
121-
}
99+
};
122100

123-
export const deleteUserHistoryEntry = async (req: AuthenticatedRequest, res: Response) => {
101+
export const deleteUserHistoryEntry = async (req: any, res: Response) => {
124102
try {
125-
const userId = extractUserIdFromToken(req);
126-
127-
if (!userId) {
128-
return res.status(401).json({ error: 'Invalid or missing token' });
129-
}
130-
103+
const userId = req.userId;
131104
const { id } = req.params;
132105

133106
const deletedEntry = await historyEntryModel.findOneAndDelete({ _id: id, userId });
134107

135108
if (!deletedEntry) {
136-
return res.status(404).json({ message: 'History entry not found' });
109+
return res.status(404).json({ message: "History entry not found" });
137110
}
138111

139-
res.status(200).json({ message: 'History entry deleted successfully' });
112+
res.status(200).json({ message: "History entry deleted successfully" });
140113
} catch (error) {
141114
res.status(500).json({ error: getErrorMessage(error) });
142115
}
143116
};
144117

145-
export const deleteUserHistoryEntries = async (req: AuthenticatedRequest, res: Response) => {
118+
export const deleteUserHistoryEntries = async (req: any, res: Response) => {
146119
try {
147-
const userId = extractUserIdFromToken(req);
148-
149-
if (!userId) {
150-
return res.status(401).json({ error: 'Invalid or missing token' });
151-
}
120+
const userId = req.userId;
152121

153122
const { ids } = req.body;
154123
if (!Array.isArray(ids)) {
@@ -162,13 +131,9 @@ export const deleteUserHistoryEntries = async (req: AuthenticatedRequest, res: R
162131
}
163132
};
164133

165-
export const deleteAllUserHistoryEntries = async (req: AuthenticatedRequest, res: Response) => {
134+
export const deleteAllUserHistoryEntries = async (req: any, res: Response) => {
166135
try {
167-
const userId = extractUserIdFromToken(req);
168-
169-
if (!userId) {
170-
return res.status(401).json({ error: 'Invalid or missing token' });
171-
}
136+
const userId = req.userId;
172137

173138
const result = await historyEntryModel.deleteMany({ userId });
174139
res.status(200).json({

backend/question-service/middlewares/auth.ts

Lines changed: 0 additions & 23 deletions
This file was deleted.

backend/question-service/middlewares/basic-access-control.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export function verifyAccessToken(req: any, res: any, next: any) {
2626
return res.status(401).json({ message: `Unauthorized: ${err.message}` });
2727
}
2828
req.isAdmin = user.isAdmin;
29+
req.userId = user.id;
2930
next();
3031
});
3132
}
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
import express from 'express';
1+
import express from "express";
22
import {
33
getUserHistoryEntries,
44
createOrUpdateUserHistoryEntry,
55
deleteUserHistoryEntry,
66
deleteUserHistoryEntries,
77
deleteAllUserHistoryEntries,
88
removeRoomIdPresence,
9-
} from '../controllers/historyController';
10-
import { authenticateToken } from '../middlewares/auth';
9+
} from "../controllers/historyController";
10+
import { verifyAccessToken } from "../middlewares/basic-access-control";
1111

1212
const router = express.Router();
1313

14-
router.get("/", authenticateToken, getUserHistoryEntries);
15-
router.post("/", authenticateToken, createOrUpdateUserHistoryEntry);
16-
router.post("/room/:id", authenticateToken, removeRoomIdPresence);
17-
router.delete("/user/:id", authenticateToken, deleteUserHistoryEntry);
18-
router.delete("/user", authenticateToken, deleteUserHistoryEntries);
19-
router.delete("/all", authenticateToken, deleteAllUserHistoryEntries);
14+
router.get("/", verifyAccessToken, getUserHistoryEntries);
15+
router.post("/", verifyAccessToken, createOrUpdateUserHistoryEntry);
16+
router.post("/room/:id", verifyAccessToken, removeRoomIdPresence);
17+
router.delete("/user/:id", verifyAccessToken, deleteUserHistoryEntry);
18+
router.delete("/user", verifyAccessToken, deleteUserHistoryEntries);
19+
router.delete("/all", verifyAccessToken, deleteAllUserHistoryEntries);
2020

2121
export default router;

0 commit comments

Comments
 (0)