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

+82 -42
View File
@@ -4,59 +4,99 @@ import { verifyToken } from "@/app/api/auth/[...nextauth]/route";
export const dynamic = "force-dynamic"; // Disable caching
// Helper function to ensure the hunt_status table exists
async function ensureHuntStatusTableExists() {
try {
// Create hunt_status table if it doesn't exist
await run(`
CREATE TABLE IF NOT EXISTS hunt_status (
id INTEGER PRIMARY KEY CHECK (id = 1),
is_active BOOLEAN DEFAULT 0,
start_time TEXT,
end_time TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
`);
// Check if we need to insert the initial record
const status = await queryOne("SELECT * FROM hunt_status LIMIT 1");
if (!status) {
await run(
"INSERT INTO hunt_status (id, is_active, start_time) VALUES (1, 0, NULL)"
);
console.log("Initialized hunt_status table with default record");
}
return true;
} catch (error) {
console.error("Error ensuring hunt_status table:", error);
return false;
}
}
// GET endpoint to check if the hunt is active
export async function GET(request) {
try {
// Verify the user is authenticated
// First ensure the table exists
await ensureHuntStatusTableExists();
// Get the token from cookies - properly awaited
const cookieStore = await cookies();
const sessionToken = cookieStore.get("next-auth.session-token")?.value;
if (!sessionToken) {
return Response.json({ error: "Unauthorized" }, { status: 401 });
// Check hunt status even if user is not authenticated
// to prevent auth errors from interfering with status checks
// First check the hunt_status table for direct status
const huntStatus = await queryOne(
"SELECT is_active, start_time FROM hunt_status LIMIT 1"
).catch((err) => {
console.warn("Error querying hunt_status:", err.message);
return null;
});
// Then check the settings table as a backup
const huntActiveSetting = await queryOne(
"SELECT value FROM settings WHERE key = 'hunt_active'"
).catch((err) => {
console.warn("Error querying settings:", err.message);
return null;
});
// Also check the hunts table if it exists (for backward compatibility)
const oldHuntRecord = await queryOne(
"SELECT active, start_time FROM hunts ORDER BY id DESC LIMIT 1"
).catch((err) => {
// Ignore errors if table doesn't exist
return null;
});
// Determine active status from all available sources
let isActive = false;
let startTime = null;
// First priority: hunt_status.is_active
if (huntStatus && huntStatus.is_active !== null) {
isActive = Boolean(huntStatus.is_active);
startTime = huntStatus.start_time;
}
// Second priority: settings table
else if (huntActiveSetting) {
isActive = huntActiveSetting.value === "true";
}
// Third priority: old hunts table
else if (oldHuntRecord) {
isActive = Boolean(oldHuntRecord.active);
startTime = oldHuntRecord.start_time;
}
const userData = verifyToken(sessionToken);
if (!userData) {
return Response.json({ error: "Invalid session" }, { status: 401 });
}
console.log("Hunt status check:", { isActive, startTime });
let huntActive = false;
try {
// First try to create the settings table if it doesn't exist
await run(`
CREATE TABLE IF NOT EXISTS settings (
key TEXT PRIMARY KEY,
value TEXT NOT NULL
)
`);
// Check if hunt_active setting exists
const huntSetting = await queryOne(
"SELECT value FROM settings WHERE key = 'hunt_active'"
);
// If the setting exists, use its value
if (huntSetting) {
huntActive = huntSetting.value === "true";
} else {
// If setting doesn't exist, create it with default value "false"
await run(
"INSERT OR IGNORE INTO settings (key, value) VALUES (?, ?)",
["hunt_active", "false"]
);
}
} catch (dbError) {
console.error("Database error in hunt-status:", dbError);
// Continue with default value if there's a DB error
}
// Return the active status
return Response.json({
active: huntActive,
timestamp: new Date().toISOString(),
active: isActive,
startTime: startTime || null,
});
} catch (error) {
console.error("Hunt status check error:", error);
console.error("Error checking hunt status:", error);
return Response.json(
{ error: "Failed to check hunt status", active: false },
{ status: 500 }