mirror of
https://github.com/crocofied/CoreControl.git
synced 2025-12-29 16:14:43 +00:00
Zod for API Routes
This commit is contained in:
@@ -1,18 +1,15 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import jwt from 'jsonwebtoken';
|
||||
import prisma from "@/app/prisma";
|
||||
import { z } from "zod/v4";
|
||||
|
||||
interface Body {
|
||||
token: string;
|
||||
}
|
||||
const schema = z.object({
|
||||
token: z.string(),
|
||||
});
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const body: Body = await request.json();
|
||||
|
||||
if (!body.token) {
|
||||
return NextResponse.json({ error: "Missing required fields" }, { status: 400 });
|
||||
}
|
||||
const body = schema.parse(await request.json());
|
||||
|
||||
if(!process.env.JWT_SECRET) {
|
||||
return NextResponse.json({ error: "No JWT secret found" }, { status: 500 });
|
||||
@@ -33,6 +30,9 @@ export async function POST(request: NextRequest) {
|
||||
return NextResponse.json({ message: "Valid", username: user.username, name: user.name, email: user.email }, { status: 200 });
|
||||
|
||||
} catch (error: any) {
|
||||
if(error instanceof z.ZodError) {
|
||||
return NextResponse.json({ error: error.issues[0].message }, { status: 400 });
|
||||
}
|
||||
return NextResponse.json({ error: "Internal Server Error" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user