From 26869d790e8fba12e742e6342f0348d3421f11a2 Mon Sep 17 00:00:00 2001 From: mudabbir-ahmad Date: Tue, 11 Mar 2025 21:50:59 +0000 Subject: [PATCH] fixed a bit --- app/(auth)/register/page.js | 13 +++- app/api/teams/create/route.js | 119 +++++++++++++++++++--------------- 2 files changed, 77 insertions(+), 55 deletions(-) diff --git a/app/(auth)/register/page.js b/app/(auth)/register/page.js index 74b5dc8..f747cfb 100644 --- a/app/(auth)/register/page.js +++ b/app/(auth)/register/page.js @@ -23,6 +23,13 @@ export default function Register() { body: JSON.stringify({ username, password }), }); + if (!response.ok) { + const result = await response.json(); + setError(result.error || "Failed to register"); + setIsLoading(false); + return; + } + const result = await response.json(); if (result.error) { @@ -44,7 +51,11 @@ export default function Register() { } } } catch (err) { - setError("An unexpected error occurred"); + if (err instanceof SyntaxError) { + setError("Unexpected response from server"); + } else { + setError("An unexpected error occurred"); + } console.error(err); setIsLoading(false); } diff --git a/app/api/teams/create/route.js b/app/api/teams/create/route.js index 31191ba..569056c 100644 --- a/app/api/teams/create/route.js +++ b/app/api/teams/create/route.js @@ -1,59 +1,70 @@ -import { query, queryOne, run } from '../../../../lib/db'; -import { getServerSession } from 'next-auth/next'; -import { authOptions } from '../../auth/[...nextauth]/route'; -import { nanoid } from 'nanoid'; +import { queryOne, run } from "../../../../lib/db"; +import { getServerSession } from "next-auth/next"; +import { authOptions } from "../../auth/[...nextauth]/route"; +import { nanoid } from "nanoid"; export async function POST(request) { - try { - const session = await getServerSession(authOptions); + try { + const session = await getServerSession(authOptions); - // Debugging: Log the session - console.log('Session:', session); + // Debugging: Log the session + console.log("Session:", session); - if (!session) { - return Response.json({ success: false, error: 'Unauthorised' }, { status: 401 }); - } - - // Ensure the user is not an admin - if (session.user.role === 'admin') { - return Response.json({ success: false, error: 'Admins cannot create teams' }, { status: 403 }); - } - - const { name, userId } = await request.json(); - - if (!name || name.trim() === '') { - return Response.json({ success: false, error: 'Team name is required' }); - } - - // Generate a unique team code (6 characters) - const teamCode = nanoid(6).toUpperCase(); - - // Insert the new team - const result = await run( - 'INSERT INTO teams (name, code) VALUES (?, ?)', - [name, teamCode] - ); - - if (!result || !result.lastID) { - return Response.json({ success: false, error: 'Failed to create team' }); - } - - // Update the user's team_id - await run( - 'UPDATE users SET team_id = ? WHERE id = ?', - [result.lastID, userId] - ); - - return Response.json({ - success: true, - teamId: result.lastID, - teamCode - }); - } catch (error) { - console.error('Create team error:', error); - return Response.json({ - success: false, - error: 'Failed to create team: ' + error.message - }); + if (!session) { + return new Response( + JSON.stringify({ success: false, error: "Unauthorised" }), + { status: 401 } + ); } -} \ No newline at end of file + + // Ensure the user is not an admin + if (session.user.role === "admin") { + return new Response( + JSON.stringify({ success: false, error: "Admins cannot create teams" }), + { status: 403 } + ); + } + + const { name, userId } = await request.json(); + + if (!name || name.trim() === "") { + return new Response( + JSON.stringify({ success: false, error: "Team name is required" }), + { status: 400 } + ); + } + + // Generate a unique team code (6 characters) + const teamCode = nanoid(6).toUpperCase(); + + // Insert the new team + const result = await run("INSERT INTO teams (name, code) VALUES (?, ?)", [ + name, + teamCode, + ]); + + // Fetch the newly created team ID + const newTeam = await queryOne("SELECT id FROM teams WHERE code = ?", [ + teamCode, + ]); + + // Update the user's team_id + await run("UPDATE users SET team_id = ? WHERE id = ?", [ + newTeam.id, + userId, + ]); + + // Update the session with the new teamId + session.user.teamId = newTeam.id; + + return new Response(JSON.stringify({ success: true, teamId: newTeam.id }), { + status: 201, + }); + } catch (error) { + console.error("Team creation error:", error); + return new Response( + JSON.stringify({ success: false, error: "Failed to create team" }), + { status: 500 } + ); + } +}