fixed a bit

This commit is contained in:
bobbert committed 2025-03-11 23:02:22 +00:00
1 parent 45b0f98971
commit a0fb2b9970
9 files changed
+314 -177

No files matched your search

+45
View File
@@ -0,0 +1,45 @@
import { queryOne, run } from "../../../lib/db";
const bcrypt = require("bcryptjs");
export async function POST(request) {
try {
const { username, password } = await request.json();
// Validate input
if (!username || !password) {
return new Response(
JSON.stringify({ error: "Username and password are required" }),
{ status: 400 }
);
}
// Check if username exists
const existingUser = await queryOne(
"SELECT id FROM users WHERE username = ?",
[username]
);
if (existingUser) {
return new Response(
JSON.stringify({ error: "Username already exists" }),
{ status: 400 }
);
}
// Hash password
const hashedPassword = await bcrypt.hash(password, 10);
// Insert user
await run(
"INSERT INTO users (username, password, role) VALUES (?, ?, 'user')",
[username, hashedPassword]
);
return new Response(JSON.stringify({ success: true }), { status: 201 });
} catch (error) {
console.error("Registration error:", error);
return new Response(JSON.stringify({ error: "Failed to register user" }), {
status: 500,
});
}
}