2025-04-24 11:36:30 -07:00
|
|
|
const studentRouter = require("express").Router();
|
|
|
|
|
const passport = require("passport");
|
|
|
|
|
const axios = require("axios");
|
2025-05-02 12:41:41 -07:00
|
|
|
const bcrypt = require("bcrypt");
|
|
|
|
|
require("dotenv").config();
|
|
|
|
|
const DB_ASSIGNMENT_SERVICE_URL = process.env.DB_ASSIGNMENT_SERVICE_URL;
|
2025-04-24 11:36:30 -07:00
|
|
|
|
|
|
|
|
|
2025-05-02 12:41:41 -07:00
|
|
|
studentRouter.post("/save", (req, res) => {});
|
|
|
|
|
|
|
|
|
|
studentRouter.post("/deploy", (req, res) => {});
|
|
|
|
|
|
2025-05-05 18:49:15 -07:00
|
|
|
studentRouter.get("/assignment/:qrnum", (req, res) => {
|
|
|
|
|
const qrnum = req.params.qrnum;
|
|
|
|
|
console.log("Fetching details for qr number:", qrnum);
|
2025-05-05 14:50:05 -07:00
|
|
|
axios
|
2025-05-05 18:49:15 -07:00
|
|
|
.get(`${DB_ASSIGNMENT_SERVICE_URL}/assignments/${qrnum}`)
|
2025-05-05 14:50:05 -07:00
|
|
|
.then((response) => {
|
|
|
|
|
console.log("Response from DB_ASSIGNMENT_SERVICE_URL:", response.data);
|
|
|
|
|
res.status(response.status).json(response.data);
|
|
|
|
|
})
|
|
|
|
|
.catch((error) => {
|
|
|
|
|
console.error("Error fetching assignment details:", error.message);
|
|
|
|
|
res.status(error.response?.status || 500).json({ error: error.message });
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2025-05-02 15:06:00 -07:00
|
|
|
studentRouter.post("/verify", async (req, res) => {
|
2025-05-02 12:41:41 -07:00
|
|
|
try {
|
2025-05-05 18:49:15 -07:00
|
|
|
const qrNumber = req.body.qrNumber;
|
2025-05-02 12:41:41 -07:00
|
|
|
const password = req.body.password;
|
2025-05-02 15:06:00 -07:00
|
|
|
console.log("Received request to verify assignment.");
|
|
|
|
|
console.log("Request body:", req.body);
|
2025-05-02 12:41:41 -07:00
|
|
|
console.log(
|
2025-05-05 18:49:15 -07:00
|
|
|
"Accessing assignment with QR Number:",
|
|
|
|
|
qrNumber,
|
2025-05-02 12:41:41 -07:00
|
|
|
"and password:",
|
|
|
|
|
password
|
|
|
|
|
);
|
|
|
|
|
|
2025-05-05 18:49:15 -07:00
|
|
|
console.log(`Fetching from URL: ${DB_ASSIGNMENT_SERVICE_URL}/assignments/${qrNumber}`);
|
2025-05-02 12:41:41 -07:00
|
|
|
const response = await axios.get(
|
2025-05-05 18:49:15 -07:00
|
|
|
`${DB_ASSIGNMENT_SERVICE_URL}/assignments/${qrNumber}`
|
2025-05-02 12:41:41 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
console.log("Response from DB_ASSIGNMENT_SERVICE_URL:", response.data);
|
|
|
|
|
console.log("Password provided:", password);
|
2025-05-06 10:43:48 -07:00
|
|
|
console.log("Password hash from database:", response.data.passwordhash);
|
2025-05-02 12:41:41 -07:00
|
|
|
|
|
|
|
|
const isPasswordValid = await bcrypt.compare(
|
|
|
|
|
password,
|
2025-05-06 10:43:48 -07:00
|
|
|
response.data.passwordhash
|
2025-05-02 12:41:41 -07:00
|
|
|
);
|
|
|
|
|
|
2025-05-02 15:06:00 -07:00
|
|
|
console.log("Password validation result:", isPasswordValid);
|
|
|
|
|
|
2025-05-02 12:41:41 -07:00
|
|
|
if (!isPasswordValid || !response.data) {
|
2025-05-02 15:06:00 -07:00
|
|
|
console.log("Invalid id or password.");
|
2025-05-02 12:41:41 -07:00
|
|
|
return res.status(401).json({ error: "Invalid id and password" });
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-02 15:06:00 -07:00
|
|
|
console.log("Verification successful. Sending response.");
|
2025-05-02 12:41:41 -07:00
|
|
|
res.status(response.status).json(response.data);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Error fetching assignment details:", error.message);
|
2025-05-02 15:06:00 -07:00
|
|
|
console.error("Error details:", error);
|
2025-05-02 12:41:41 -07:00
|
|
|
res.status(error.response?.status || 500).json({ error: error.message });
|
|
|
|
|
}
|
2025-04-24 11:36:30 -07:00
|
|
|
});
|
|
|
|
|
|
2025-05-02 12:41:41 -07:00
|
|
|
module.exports = studentRouter;
|