added map page files and fixed code so that when an admin starts the timer the user can move on and isnt accidentally being redirected to the page to show that the timer isn't active even though it is

This commit is contained in:
bobbert committed 2025-03-14 15:39:05 +00:00
1 parent 6b233c90e7
commit 6ad943a868
7 files changed
+965 -39

No files matched your search

+65
View File
@@ -0,0 +1,65 @@
import { queryOne, run } from "@/lib/db";
import { cookies } from "next/headers";
import { verifyToken } from "@/app/api/auth/[...nextauth]/route";
export const dynamic = "force-dynamic"; // Disable caching
// GET endpoint to retrieve a specific clue
export async function GET(request, { params }) {
const { clueId } = params;
try {
// Verify the user is authenticated
const cookieStore = await cookies();
const sessionToken = cookieStore.get("next-auth.session-token")?.value;
if (!sessionToken) {
return Response.json(
{ error: "Unauthorized", clue: null },
{ status: 401 }
);
}
const userData = verifyToken(sessionToken);
if (!userData) {
return Response.json(
{ error: "Invalid session", clue: null },
{ status: 401 }
);
}
// Check if the hunt is active
const huntActiveSetting = await queryOne(
"SELECT value FROM settings WHERE key = 'hunt_active'"
);
const huntActive = huntActiveSetting && huntActiveSetting.value === "true";
if (!huntActive) {
// Return a valid JSON response with clue set to null
return Response.json({
error: "Hunt is not active",
clue: null,
});
}
// Fetch the specific clue
const clue = await queryOne("SELECT * FROM clues WHERE id = ?", [clueId]);
if (!clue) {
return Response.json(
{ error: "Clue not found", clue: null },
{ status: 404 }
);
}
return Response.json({ clue });
} catch (error) {
console.error("Error fetching clue details:", error);
// Always return a valid JSON response even on error
return Response.json(
{ error: "Failed to fetch clue details: " + error.message, clue: null },
{ status: 500 }
);
}
}
+104
View File
@@ -0,0 +1,104 @@
import { query, queryOne, run } from "@/lib/db";
import { cookies } from "next/headers";
import { verifyToken } from "@/app/api/auth/[...nextauth]/route";
export const dynamic = "force-dynamic"; // Disable caching
// Helper function to ensure the clues table exists (if not already created elsewhere)
async function ensureCluesTableExists() {
try {
// Create the clues table if it doesn't exist
await run(`
CREATE TABLE IF NOT EXISTS clues (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
description TEXT NOT NULL,
location TEXT,
order_number INTEGER,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
`);
return true;
} catch (error) {
console.error("Error ensuring clues table exists:", error);
return false;
}
}
// GET endpoint to retrieve all clues
export async function GET(request) {
try {
// Verify the user is authenticated
const cookieStore = await cookies();
const sessionToken = cookieStore.get("next-auth.session-token")?.value;
if (!sessionToken) {
return Response.json(
{ error: "Unauthorized", clues: [] },
{ status: 401 }
);
}
const userData = verifyToken(sessionToken);
if (!userData) {
return Response.json(
{ error: "Invalid session", clues: [] },
{ status: 401 }
);
}
// Ensure the clues table exists
await ensureCluesTableExists();
// Check if the hunt is active
const huntActiveSetting = await queryOne(
"SELECT value FROM settings WHERE key = 'hunt_active'"
);
const huntActive = huntActiveSetting && huntActiveSetting.value === "true";
if (!huntActive) {
// Return empty clues array instead of error status to avoid redirect issues
return Response.json({
error: "Hunt is not active",
clues: [],
});
}
// Fetch all clues
const clues = await query("SELECT * FROM clues ORDER BY order_number ASC");
// If no clues are found, insert sample clues (for testing purposes)
if (!clues || clues.length === 0) {
// Insert sample clues
await run(
"INSERT INTO clues (title, description, location, order_number) VALUES (?, ?, ?, ?)",
["First Clue", "This is the first clue description", "Location 1", 1]
);
await run(
"INSERT INTO clues (title, description, location, order_number) VALUES (?, ?, ?, ?)",
["Second Clue", "This is the second clue description", "Location 2", 2]
);
await run(
"INSERT INTO clues (title, description, location, order_number) VALUES (?, ?, ?, ?)",
["Final Clue", "This is the final clue description", "Location 3", 3]
);
// Fetch the inserted clues
const sampleClues = await query(
"SELECT * FROM clues ORDER BY order_number ASC"
);
return Response.json({ clues: sampleClues });
}
return Response.json({ clues });
} catch (error) {
console.error("Error fetching clues:", error);
// Always return a valid JSON response even on error
return Response.json(
{ error: "Failed to fetch clues: " + error.message, clues: [] },
{ status: 500 }
);
}
}