Files
peworkshop/app/api/clues/count/route.js
T
2025-03-16 21:40:28 +00:00

44 lines
1.2 KiB
JavaScript

import { queryOne } from "@/lib/db";
import { getToken } from "next-auth/jwt";
export const dynamic = "force-dynamic"; // No caching
export async function GET(request) {
try {
// Verify token
const token = await getToken({
req: request,
secret:
process.env.NEXTAUTH_SECRET ||
"your-fallback-secret-should-be-at-least-32-chars",
});
if (!token) {
return Response.json({ error: "Unauthorized access" }, { status: 401 });
}
// Get the total count of clues with a direct SQL COUNT query
const result = await queryOne("SELECT COUNT(*) as count FROM clues");
if (!result) {
return Response.json({ error: "Failed to count clues" }, { status: 500 });
}
// Log the count to help with debugging
console.log(`API: Returning clue count: ${result.count}`);
return Response.json(
{ count: result.count },
{
headers: {
"Cache-Control": "no-store, no-cache, must-revalidate",
Pragma: "no-cache",
},
}
);
} catch (error) {
console.error("Error counting clues:", error);
return Response.json({ error: "Server error" }, { status: 500 });
}
}