Skip to content

Commit

Permalink
finished admin-scan
Browse files Browse the repository at this point in the history
  • Loading branch information
sdagg9 committed Jun 5, 2024
1 parent 05dedc7 commit 47cc597
Show file tree
Hide file tree
Showing 3 changed files with 205 additions and 80 deletions.
14 changes: 11 additions & 3 deletions src/services/admin/admin-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,23 @@ dotenv.config();
const adminRouter = Router();

adminRouter.post(
"/scan",
"/scan/",
RoleChecker([Role.Enum.ADMIN]),
async (req, res, next) => {
try {
const { qrCode } = req.body;
if (!qrCode) {
console.log("made it to if");
return res
.status(StatusCodes.BAD_REQUEST)
.json({ error: "QR code is required" });
}
const { userId, expTime } = validateQrHash(qrCode);

if (Date.now() / 1000 > expTime) {
return res.status(StatusCodes.UNAUTHORIZED).json({ error: "QR code has expired" });
return res
.status(StatusCodes.UNAUTHORIZED)
.json({ error: "QR code has expired" });
}

const user = await Database.ATTENDEES.findOne({ userId });
Expand All @@ -37,4 +45,4 @@ adminRouter.post(
}
);

export default adminRouter;
export default adminRouter;
14 changes: 9 additions & 5 deletions src/services/admin/admin-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@ export function generateQrHash(userId: string, expTime: number) {
}

export function validateQrHash(qrCode: string) {
const [hashStr, expTime, userId] = qrCode.split('#');
const generatedHash = generateQrHash(userId, parseInt(expTime));
if (generatedHash.split('#')[0] !== hashStr) {
const parts = qrCode.split("#");
const userId = parts[2];
const expTime = parseInt(parts[1]);
const generatedHash = generateQrHash(userId, expTime);

if (generatedHash.split("#")[0] !== parts[0]) {
throw new Error("Invalid QR code");
}
return { userId, expTime: parseInt(expTime) };
}

return { userId, expTime };
}
Loading

0 comments on commit 47cc597

Please sign in to comment.