Create User API Route

This commit is contained in:
headlesdev 2025-05-17 14:51:16 +02:00
parent 735deb9efe
commit e9fe5bb22a
2 changed files with 39 additions and 0 deletions

View File

@ -0,0 +1,34 @@
import { NextRequest, NextResponse } from "next/server";
import prisma from "@/app/prisma";
interface Body {
username: string;
name: string;
email: string;
password: string;
}
export async function POST(request: NextRequest) {
try {
const body: Body = await request.json();
if (!body.username || !body.name || !body.email || !body.password) {
return NextResponse.json({ error: "Missing required fields" }, { status: 400 });
}
const user = await prisma.user.create({
data: {
username: body.username,
name: body.name,
email: body.email,
password: body.password,
},
});
return NextResponse.json({ user }, { status: 201 });
} catch (error: any) {
return NextResponse.json({ error: "Internal Server Error" }, { status: 500 });
}
}

5
app/prisma.ts Normal file
View File

@ -0,0 +1,5 @@
import { PrismaClient } from "@/prisma/generated/prisma";
let prisma = new PrismaClient();
export default prisma;