fixed a bit of stuff the current but API is broken again for logging in so will be fixing that with the next push

This commit is contained in:
bobbert committed 2025-03-14 19:33:13 +00:00
1 parent 6ad943a868
commit ea38775a82
20 files changed
+1643 -1027

No files matched your search

+35 -30
View File
@@ -9,38 +9,41 @@ 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
// Check if the hunt is active first
const huntActiveSetting = await queryOne(
"SELECT value FROM settings WHERE key = 'hunt_active'"
);
const huntActive = huntActiveSetting && huntActiveSetting.value === "true";
// Only verify authentication when hunt is not active
if (!huntActive) {
// Return a valid JSON response with clue set to null
return Response.json({
error: "Hunt is not active",
clue: null,
});
const cookieStore = await cookies();
const sessionToken =
cookieStore.get("next-auth.session-token")?.value ||
cookieStore.get("__Secure-next-auth.session-token")?.value;
if (!sessionToken) {
return Response.json(
{ error: "Unauthorized", clue: null },
{ status: 401, headers: { "Content-Type": "application/json" } }
);
}
const userData = verifyToken(sessionToken);
if (!userData) {
return Response.json(
{ error: "Invalid session", clue: null },
{ status: 401, headers: { "Content-Type": "application/json" } }
);
}
// Admin can see clues even if hunt is not active
if (userData.role !== "admin") {
return Response.json(
{ error: "Hunt is not active", clue: null },
{ headers: { "Content-Type": "application/json" } }
);
}
}
// Fetch the specific clue
@@ -49,17 +52,19 @@ export async function GET(request, { params }) {
if (!clue) {
return Response.json(
{ error: "Clue not found", clue: null },
{ status: 404 }
{ status: 404, headers: { "Content-Type": "application/json" } }
);
}
return Response.json({ clue });
return Response.json(
{ clue },
{ headers: { "Content-Type": "application/json" } }
);
} 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 }
{ status: 500, headers: { "Content-Type": "application/json" } }
);
}
}
+71 -48
View File
@@ -4,21 +4,18 @@ 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)
// Helper function to ensure the clues table exists
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);
@@ -26,79 +23,105 @@ async function ensureCluesTableExists() {
}
}
// Helper function to extract numeric value from clue title
function extractClueNumber(title) {
// Look for patterns like "Clue 1", "Clue 10", etc.
const match = title.match(/Clue\s+(\d+)/i);
// If we find a match, return the number, otherwise return Infinity (to sort to the end)
return match ? parseInt(match[1], 10) : Infinity;
}
// 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;
// First 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 (!sessionToken) {
return Response.json(
{ error: "Unauthorized", clues: [] },
{ status: 401 }
);
}
// If hunt is active, allow access without authentication
if (!huntActive) {
// Only verify authentication when hunt is not active
const cookieStore = await cookies();
const sessionToken =
cookieStore.get("next-auth.session-token")?.value ||
cookieStore.get("__Secure-next-auth.session-token")?.value;
const userData = verifyToken(sessionToken);
if (!userData) {
return Response.json(
{ error: "Invalid session", clues: [] },
{ status: 401 }
);
if (!sessionToken) {
return Response.json(
{ error: "Unauthorized", clues: [] },
{ status: 401, headers: { "Content-Type": "application/json" } }
);
}
const userData = verifyToken(sessionToken);
if (!userData) {
return Response.json(
{ error: "Invalid session", clues: [] },
{ status: 401, headers: { "Content-Type": "application/json" } }
);
}
// Admin can see clues even if hunt is not active
if (userData.role !== "admin") {
return Response.json(
{ error: "Hunt is not active", clues: [] },
{ headers: { "Content-Type": "application/json" } }
);
}
}
// 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");
// Fetch all clues without sorting in SQL
const clues = await query("SELECT * FROM clues");
// 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]
"INSERT INTO clues (title, description, location) VALUES (?, ?, ?)",
["Clue 1", "This is the first clue description", "Location 1"]
);
await run(
"INSERT INTO clues (title, description, location, order_number) VALUES (?, ?, ?, ?)",
["Second Clue", "This is the second clue description", "Location 2", 2]
"INSERT INTO clues (title, description, location) VALUES (?, ?, ?)",
["Clue 2", "This is the second clue description", "Location 2"]
);
await run(
"INSERT INTO clues (title, description, location, order_number) VALUES (?, ?, ?, ?)",
["Final Clue", "This is the final clue description", "Location 3", 3]
"INSERT INTO clues (title, description, location) VALUES (?, ?, ?)",
["Clue 3", "This is the final clue description", "Location 3"]
);
// Fetch the inserted clues
const sampleClues = await query(
"SELECT * FROM clues ORDER BY order_number ASC"
const sampleClues = await query("SELECT * FROM clues");
// Sort clues based on numbering in title
sampleClues.sort(
(a, b) => extractClueNumber(a.title) - extractClueNumber(b.title)
);
return Response.json(
{ clues: sampleClues },
{ headers: { "Content-Type": "application/json" } }
);
return Response.json({ clues: sampleClues });
}
return Response.json({ clues });
// Sort clues based on numbering in title
clues.sort(
(a, b) => extractClueNumber(a.title) - extractClueNumber(b.title)
);
return Response.json(
{ clues },
{ headers: { "Content-Type": "application/json" } }
);
} 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 }
{ status: 500, headers: { "Content-Type": "application/json" } }
);
}
}