diff --git a/backend/server.js b/backend/server.js index 79cff17..7a57300 100644 --- a/backend/server.js +++ b/backend/server.js @@ -95,11 +95,21 @@ async function startServer() { app.post('/api/clock', authenticateJWT, async (req, res) => { try { const { userId, eventType, qrCodeValue, latitude, longitude } = req.body + const [qrRows] = await db.execute('SELECT name, is_active FROM qr_codes WHERE id = ?', [ qrCodeValue, ]) - if (qrRows.length === 0 || !qrRows[0].is_active) { - return res.status(400).json({ message: 'Invalid or inactive QR Code.' }) + + if (qrRows.length === 0) { + // This code is not in the database at all. + return res.status(400).json({ message: 'Invalid QR Code scanned.' }) + } + + if (!qrRows[0].is_active) { + // This code exists but has been deactivated. + return res + .status(400) + .json({ message: 'This QR Code has expired and is no longer active.' }) } const [lastEventRows] = await db.execute( 'SELECT event_type FROM clock_records WHERE worker_id = ? ORDER BY timestamp DESC LIMIT 1', @@ -204,8 +214,8 @@ async function startServer() { if (!username || !password || !fullName) { return res.status(400).json({ message: 'Username, password, and full name are required.' }) } - const saltRounds = 10; - const hashedPassword = await bcrypt.hash(password, saltRounds); + const saltRounds = 10 + const hashedPassword = await bcrypt.hash(password, saltRounds) const [result] = await db.execute( "INSERT INTO workers (username, password_hash, full_name, role) VALUES (?, ?, ?, 'worker')", [username, hashedPassword, fullName], diff --git a/src/api.js b/src/api.js index 1acb299..f6e6ec8 100644 --- a/src/api.js +++ b/src/api.js @@ -19,7 +19,12 @@ export async function apiFetch(endpoint, options = {}) { }) if (!response.ok) { - throw new Error(`API call failed with status: ${response.status}`) + // Try to parse the error response body from the server + const errorData = await response.json() + throw new Error(errorData.message || `API call failed with status: ${response.status}`) + } + if (response.status === 204) { + return null } return response.json() diff --git a/src/components/AttendanceReporting.vue b/src/components/AttendanceReporting.vue index d7888b5..8beda09 100644 --- a/src/components/AttendanceReporting.vue +++ b/src/components/AttendanceReporting.vue @@ -223,6 +223,8 @@