fixed a bit

This commit is contained in:
bobbert committed 2025-03-11 21:50:59 +00:00
1 parent 494f42a4c3
commit 26869d790e
2 files changed
+52 -30

No files matched your search

+11
View File
@@ -23,6 +23,13 @@ export default function Register() {
body: JSON.stringify({ username, password }), 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(); const result = await response.json();
if (result.error) { if (result.error) {
@@ -44,7 +51,11 @@ export default function Register() {
} }
} }
} catch (err) { } catch (err) {
if (err instanceof SyntaxError) {
setError("Unexpected response from server");
} else {
setError("An unexpected error occurred"); setError("An unexpected error occurred");
}
console.error(err); console.error(err);
setIsLoading(false); setIsLoading(false);
} }
+41 -30
View File
@@ -1,59 +1,70 @@
import { query, queryOne, run } from '../../../../lib/db'; import { queryOne, run } from "../../../../lib/db";
import { getServerSession } from 'next-auth/next'; import { getServerSession } from "next-auth/next";
import { authOptions } from '../../auth/[...nextauth]/route'; import { authOptions } from "../../auth/[...nextauth]/route";
import { nanoid } from 'nanoid'; import { nanoid } from "nanoid";
export async function POST(request) { export async function POST(request) {
try { try {
const session = await getServerSession(authOptions); const session = await getServerSession(authOptions);
// Debugging: Log the session // Debugging: Log the session
console.log('Session:', session); console.log("Session:", session);
if (!session) { if (!session) {
return Response.json({ success: false, error: 'Unauthorised' }, { status: 401 }); return new Response(
JSON.stringify({ success: false, error: "Unauthorised" }),
{ status: 401 }
);
} }
// Ensure the user is not an admin // Ensure the user is not an admin
if (session.user.role === 'admin') { if (session.user.role === "admin") {
return Response.json({ success: false, error: 'Admins cannot create teams' }, { status: 403 }); return new Response(
JSON.stringify({ success: false, error: "Admins cannot create teams" }),
{ status: 403 }
);
} }
const { name, userId } = await request.json(); const { name, userId } = await request.json();
if (!name || name.trim() === '') { if (!name || name.trim() === "") {
return Response.json({ success: false, error: 'Team name is required' }); return new Response(
JSON.stringify({ success: false, error: "Team name is required" }),
{ status: 400 }
);
} }
// Generate a unique team code (6 characters) // Generate a unique team code (6 characters)
const teamCode = nanoid(6).toUpperCase(); const teamCode = nanoid(6).toUpperCase();
// Insert the new team // Insert the new team
const result = await run( const result = await run("INSERT INTO teams (name, code) VALUES (?, ?)", [
'INSERT INTO teams (name, code) VALUES (?, ?)', name,
[name, teamCode] teamCode,
); ]);
if (!result || !result.lastID) { // Fetch the newly created team ID
return Response.json({ success: false, error: 'Failed to create team' }); const newTeam = await queryOne("SELECT id FROM teams WHERE code = ?", [
} teamCode,
]);
// Update the user's team_id // Update the user's team_id
await run( await run("UPDATE users SET team_id = ? WHERE id = ?", [
'UPDATE users SET team_id = ? WHERE id = ?', newTeam.id,
[result.lastID, userId] userId,
); ]);
return Response.json({ // Update the session with the new teamId
success: true, session.user.teamId = newTeam.id;
teamId: result.lastID,
teamCode return new Response(JSON.stringify({ success: true, teamId: newTeam.id }), {
status: 201,
}); });
} catch (error) { } catch (error) {
console.error('Create team error:', error); console.error("Team creation error:", error);
return Response.json({ return new Response(
success: false, JSON.stringify({ success: false, error: "Failed to create team" }),
error: 'Failed to create team: ' + error.message { status: 500 }
}); );
} }
} }