mirror of
https://github.com/mudabbir-ahmad/peworkshop.git
synced 2026-10-07 19:50:20 +00:00
Refactor database schema; add hunts and team_clues tables, update clues structure, and improve error handling in routes
This commit is contained in:
1 parent
4f7fdc6ad2
commit
77e83dc21c
13 files changed
+136
-111
No files matched your search
@@ -1,5 +1,7 @@
|
||||
export const runtime = "nodejs";
|
||||
// Fix: Make sure we're using the correct import
|
||||
import NextAuth from "next-auth";
|
||||
import CredentialsProvider from "next-auth/providers/credentials";
|
||||
import { validateCredentials } from "@/actions/auth";
|
||||
|
||||
// Ensure NEXTAUTH_SECRET is defined
|
||||
@@ -15,10 +17,9 @@ if (!process.env.NEXTAUTH_URL) {
|
||||
export const authOptions = {
|
||||
debug: process.env.NODE_ENV === "development",
|
||||
providers: [
|
||||
{
|
||||
CredentialsProvider({
|
||||
id: "credentials",
|
||||
name: "Credentials",
|
||||
type: "credentials",
|
||||
credentials: {
|
||||
username: { label: "Username", type: "text" },
|
||||
password: { label: "Password", type: "password" },
|
||||
@@ -43,7 +44,7 @@ export const authOptions = {
|
||||
return null;
|
||||
}
|
||||
},
|
||||
},
|
||||
}),
|
||||
],
|
||||
callbacks: {
|
||||
async jwt({ token, user }) {
|
||||
@@ -66,11 +67,9 @@ export const authOptions = {
|
||||
return session;
|
||||
},
|
||||
},
|
||||
// no "pages" property
|
||||
session: { strategy: "jwt", maxAge: 30 * 24 * 60 * 60 },
|
||||
secret: process.env.NEXTAUTH_SECRET,
|
||||
};
|
||||
|
||||
// Creating the handler after exporting authOptions
|
||||
const handler = NextAuth(authOptions);
|
||||
export { handler as GET, handler as POST };
|
||||
@@ -2,7 +2,8 @@ import { getServerSession } from "next-auth/next";
|
||||
import { authOptions } from "../auth/[...nextauth]/route";
|
||||
import { run } from "@/lib/db";
|
||||
|
||||
export async function POST(request) {
|
||||
export async function POST() {
|
||||
// Removed unused 'request' parameter
|
||||
const session = await getServerSession(authOptions);
|
||||
if (!session || session.user.role !== "admin") {
|
||||
return new Response(JSON.stringify({ error: "Unauthorized" }), {
|
||||
@@ -12,12 +13,13 @@ export async function POST(request) {
|
||||
|
||||
try {
|
||||
// Add a WHERE clause or provide an ID if you're updating a specific hunt record
|
||||
await run("UPDATE hunts SET active = 1, start_time = NOW() WHERE id = ?", [1]);
|
||||
// Or if you just have one hunt record: "UPDATE hunts SET active = 1, start_time = NOW()"
|
||||
|
||||
await run("UPDATE hunts SET active = 1, start_time = NOW() WHERE id = ?", [
|
||||
1,
|
||||
]);
|
||||
|
||||
return new Response(JSON.stringify({ success: true }), { status: 200 });
|
||||
} catch (err) {
|
||||
console.error("Hunt start error:", err); // Add logging for better debugging
|
||||
console.error("Hunt start error:", err);
|
||||
return new Response(JSON.stringify({ error: err.message }), {
|
||||
status: 500,
|
||||
});
|
||||
|
||||
@@ -2,7 +2,8 @@ import { query, queryOne } from "@/lib/db";
|
||||
import { getServerSession } from "next-auth/next";
|
||||
import { authOptions } from "../../auth/[...nextauth]/route";
|
||||
|
||||
export async function GET(request, context) {
|
||||
export async function GET(_, context) {
|
||||
// Changed 'request' to '_' to indicate unused parameter
|
||||
const session = await getServerSession(authOptions);
|
||||
|
||||
if (!session) {
|
||||
@@ -11,7 +12,7 @@ export async function GET(request, context) {
|
||||
|
||||
try {
|
||||
const teamId = context.params.teamId;
|
||||
|
||||
|
||||
// Validate teamId
|
||||
if (!teamId || isNaN(parseInt(teamId, 10))) {
|
||||
return Response.json({ error: "Invalid team ID" }, { status: 400 });
|
||||
@@ -24,16 +25,18 @@ export async function GET(request, context) {
|
||||
}
|
||||
|
||||
const members = await query(
|
||||
"SELECT id, username FROM users WHERE team_id = ?",
|
||||
"SELECT id, username FROM users WHERE team_id = ?",
|
||||
[teamId]
|
||||
);
|
||||
|
||||
return Response.json({
|
||||
success: true,
|
||||
team,
|
||||
members
|
||||
}, { status: 200 });
|
||||
|
||||
return Response.json(
|
||||
{
|
||||
success: true,
|
||||
team,
|
||||
members,
|
||||
},
|
||||
{ status: 200 }
|
||||
);
|
||||
} catch (error) {
|
||||
console.error("Team fetch error:", error);
|
||||
return Response.json(
|
||||
|
||||
Reference in new issue
Block a user