mirror of
https://github.com/mudabbir-ahmad/peworkshop.git
synced 2026-10-08 03:50:21 +00:00
fixed a bit
This commit is contained in:
1 parent
45b0f98971
commit
a0fb2b9970
9 files changed
+314
-177
No files matched your search
@@ -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,
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user