mirror of
https://github.com/mudabbir-ahmad/peworkshop.git
synced 2026-10-08 03:50:21 +00:00
issues regarding the map and hunt stopping have been fixed. leaderboard page needs to be integrated and a clues should be added to be properly processed. after that the css for the application should be developed.
This commit is contained in:
1 parent
c79a9636a7
commit
e7ad53643c
12 files changed
+855
-187
No files matched your search
+87
-33
@@ -1,6 +1,5 @@
|
||||
import { query, queryOne, run } from "@/lib/db";
|
||||
import { cookies } from "next/headers";
|
||||
import { verifyToken } from "@/app/api/auth/[...nextauth]/route";
|
||||
import { getToken } from "next-auth/jwt";
|
||||
|
||||
export const dynamic = "force-dynamic"; // Disable caching
|
||||
|
||||
@@ -90,17 +89,83 @@ async function ensureSettingsTableExists() {
|
||||
}
|
||||
}
|
||||
|
||||
// Helper function to update hunt active status
|
||||
// Helper function to update hunt active status - ensure it updates all tables
|
||||
async function updateHuntActiveStatus(isActive) {
|
||||
try {
|
||||
await ensureSettingsTableExists();
|
||||
|
||||
// Update settings table - this is used by many endpoints to check status
|
||||
await run("INSERT OR REPLACE INTO settings (key, value) VALUES (?, ?)", [
|
||||
"hunt_active",
|
||||
isActive ? "true" : "false",
|
||||
]);
|
||||
|
||||
console.log("Hunt active status updated:", isActive);
|
||||
// Update hunt_status table for consistency
|
||||
try {
|
||||
const now = new Date().toISOString();
|
||||
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, ?, ?)",
|
||||
[isActive ? 1 : 0, isActive ? now : null]
|
||||
);
|
||||
} else {
|
||||
// Update existing record
|
||||
await run(
|
||||
"UPDATE hunt_status SET is_active = ?, start_time = ? WHERE id = 1",
|
||||
[isActive ? 1 : 0, isActive ? now : null]
|
||||
);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error("Error updating hunt_status table:", err);
|
||||
}
|
||||
|
||||
// Also try to update the traditional hunts table for backwards compatibility
|
||||
try {
|
||||
const now = new Date().toISOString();
|
||||
await run(`
|
||||
CREATE TABLE IF NOT EXISTS hunts (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
active BOOLEAN DEFAULT 0,
|
||||
start_time DATETIME DEFAULT NULL,
|
||||
end_time DATETIME DEFAULT NULL,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
)
|
||||
`);
|
||||
|
||||
// Check if we need to insert a new record or update existing
|
||||
const hunt = await queryOne(
|
||||
"SELECT * FROM hunts ORDER BY id DESC LIMIT 1"
|
||||
);
|
||||
if (!hunt) {
|
||||
await run("INSERT INTO hunts (active, start_time) VALUES (?, ?)", [
|
||||
isActive ? 1 : 0,
|
||||
isActive ? now : null,
|
||||
]);
|
||||
} else {
|
||||
// Update most recent hunt record
|
||||
await run("UPDATE hunts SET active = ?, start_time = ? WHERE id = ?", [
|
||||
isActive ? 1 : 0,
|
||||
isActive ? now : null,
|
||||
hunt.id,
|
||||
]);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error("Error updating hunts table:", err);
|
||||
}
|
||||
|
||||
console.log("Hunt active status updated across ALL tables:", isActive);
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error("Failed to update hunt active status:", error);
|
||||
@@ -111,19 +176,18 @@ async function updateHuntActiveStatus(isActive) {
|
||||
// GET endpoint to get current timer status
|
||||
export async function GET(request) {
|
||||
try {
|
||||
// Verify the user is authenticated
|
||||
const cookieStore = await cookies(); // Add await here
|
||||
const sessionToken = cookieStore.get("next-auth.session-token")?.value;
|
||||
// Use proper next-auth token validation
|
||||
const token = await getToken({
|
||||
req: request,
|
||||
secret:
|
||||
process.env.NEXTAUTH_SECRET ||
|
||||
"your-fallback-secret-should-be-at-least-32-chars",
|
||||
});
|
||||
|
||||
if (!sessionToken) {
|
||||
if (!token) {
|
||||
return Response.json({ error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
const userData = verifyToken(sessionToken);
|
||||
if (!userData) {
|
||||
return Response.json({ error: "Invalid session" }, { status: 401 });
|
||||
}
|
||||
|
||||
// Ensure both tables exist
|
||||
const tableExists = await ensureTableExists();
|
||||
await ensureSettingsTableExists();
|
||||
@@ -175,31 +239,21 @@ export async function GET(request) {
|
||||
// POST endpoint to start/stop timer
|
||||
export async function POST(request) {
|
||||
try {
|
||||
// Verify the user is authenticated and is an admin
|
||||
const cookieStore = await cookies(); // Add await here
|
||||
const sessionToken = cookieStore.get("next-auth.session-token")?.value;
|
||||
|
||||
if (!sessionToken) {
|
||||
// Use next-auth's getToken to properly decode the 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" }, { status: 401 });
|
||||
}
|
||||
|
||||
const userData = verifyToken(sessionToken);
|
||||
if (!userData) {
|
||||
return Response.json({ error: "Invalid session" }, { status: 401 });
|
||||
}
|
||||
|
||||
console.log("User role:", userData.role);
|
||||
|
||||
// Check if user is admin (more lenient check)
|
||||
if (
|
||||
!(
|
||||
userData.role === "admin" ||
|
||||
userData.isAdmin === true ||
|
||||
userData.is_admin === 1
|
||||
)
|
||||
) {
|
||||
if (token.role !== "admin") {
|
||||
return Response.json(
|
||||
{ error: "Unauthorized: Admin access required" },
|
||||
{ error: "Only admin can start the timer" },
|
||||
{ status: 403 }
|
||||
);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user