diff --git a/app/api/user/login.ts/route.ts b/app/api/user/login.ts/route.ts index e69de29..90663a4 100644 --- a/app/api/user/login.ts/route.ts +++ b/app/api/user/login.ts/route.ts @@ -0,0 +1,39 @@ +import { NextRequest, NextResponse } from "next/server"; +import bcrypt from 'bcryptjs'; +import prisma from "@/app/prisma"; + +interface Body { + username: string; + password: string; +} + +export async function POST(request: NextRequest) { + try { + const body: Body = await request.json(); + + if (!body.username || !body.password) { + return NextResponse.json({ error: "Missing required fields" }, { status: 400 }); + } + + const user = await prisma.user.findUnique({ + where: { + username: body.username, + }, + }); + + if (!user) { + return NextResponse.json({ error: "User not found" }, { status: 404 }); + } + + const validPassword = await bcrypt.compare(body.password, user.password); + + if (!validPassword) { + return NextResponse.json({ error: "Invalid password" }, { status: 401 }); + } + + return NextResponse.json({ message: "Login successful" }, { status: 200 }); + + } catch (error: any) { + return NextResponse.json({ error: "Internal Server Error" }, { status: 500 }); + } +}