chore(api): add explicit type casting for request payloads
This commit is contained in:
@@ -3,15 +3,27 @@ import bcrypt from "bcryptjs";
|
||||
import { prisma } from "../../../lib/prisma";
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
const { name, email, password } = await req.json() as { name?: string; email?: string; password?: string };
|
||||
const { name, email, password } = (await req.json()) as {
|
||||
name?: string;
|
||||
email?: string;
|
||||
password?: string;
|
||||
};
|
||||
|
||||
if (!name?.trim() || !email?.trim() || !password || password.length < 6) {
|
||||
return NextResponse.json({ error: "Champs invalides (mot de passe min. 6 caractères)" }, { status: 400 });
|
||||
return NextResponse.json(
|
||||
{ error: "Champs invalides (mot de passe min. 6 caractères)" },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
|
||||
const existing = await prisma.user.findUnique({ where: { email: email.toLowerCase() } });
|
||||
const existing = await prisma.user.findUnique({
|
||||
where: { email: email.toLowerCase() },
|
||||
});
|
||||
if (existing) {
|
||||
return NextResponse.json({ error: "Cet email est déjà utilisé" }, { status: 409 });
|
||||
return NextResponse.json(
|
||||
{ error: "Cet email est déjà utilisé" },
|
||||
{ status: 409 },
|
||||
);
|
||||
}
|
||||
|
||||
const hashed = await bcrypt.hash(password, 10);
|
||||
@@ -19,5 +31,8 @@ export async function POST(req: NextRequest) {
|
||||
data: { name: name.trim(), email: email.toLowerCase(), password: hashed },
|
||||
});
|
||||
|
||||
return NextResponse.json({ id: user.id, name: user.name, email: user.email }, { status: 201 });
|
||||
return NextResponse.json(
|
||||
{ id: user.id, name: user.name, email: user.email },
|
||||
{ status: 201 },
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user