Files
peworkshop/app/api/clues/[clueId]/route.js
T

66 lines
1.8 KiB
JavaScript

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 }
);
}
}