mirror of
https://github.com/mudabbir-ahmad/peworkshop.git
synced 2026-10-07 19:50:20 +00:00
66 lines
1.9 KiB
JavaScript
66 lines
1.9 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
|
|
|
|
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" }, { status: 401 });
|
|
}
|
|
|
|
const userData = verifyToken(sessionToken);
|
|
if (!userData) {
|
|
return Response.json({ error: "Invalid session" }, { status: 401 });
|
|
}
|
|
|
|
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(),
|
|
});
|
|
} catch (error) {
|
|
console.error("Hunt status check error:", error);
|
|
return Response.json(
|
|
{ error: "Failed to check hunt status", active: false },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|