Login Route with JWT

This commit is contained in:
headlesdev
2025-05-17 15:07:22 +02:00
parent 84f1c16529
commit 0cd01e2c4e
3 changed files with 154 additions and 4 deletions

View File

@@ -1,5 +1,6 @@
import { NextRequest, NextResponse } from "next/server";
import bcrypt from 'bcryptjs';
import jwt from 'jsonwebtoken';
import prisma from "@/app/prisma";
interface Body {
@@ -31,7 +32,13 @@ export async function POST(request: NextRequest) {
return NextResponse.json({ error: "Invalid password" }, { status: 401 });
}
return NextResponse.json({ message: "Login successful" }, { status: 200 });
if(!process.env.JWT_SECRET) {
return NextResponse.json({ error: "No JWT secret found" }, { status: 500 });
}
const token = jwt.sign({ id: user.id }, process.env.JWT_SECRET, { expiresIn: "7d" });
return NextResponse.json({ message: "Login successful", token }, { status: 200 });
} catch (error: any) {
return NextResponse.json({ error: "Internal Server Error" }, { status: 500 });