import { NextResponse } from "next/server"; import { verifyToken } from "./lib/auth"; export async function middleware(request) { const path = request.nextUrl.pathname; if (path.startsWith("/api/admin")) { const token = request.cookies.get("token")?.value; if (!token) { return NextResponse.json({ error: "Not authenticated" }, { status: 401 }); } try { const decoded = await verifyToken(token); if (decoded.role !== "admin") { return NextResponse.json({ error: "Not authorized" }, { status: 403 }); } } catch (error) { return NextResponse.json({ error: "Invalid token" }, { status: 401 }); } } return NextResponse.next(); } export const config = { matcher: "/api/admin/:path*", };