This commit is contained in:
sbb45
2026-06-02 17:45:29 +05:00
commit 475c7cc73c
155 changed files with 15164 additions and 0 deletions
+30
View File
@@ -0,0 +1,30 @@
import { NextRequest, NextResponse } from "next/server";
import { jwtVerify } from "jose";
const JWT_SECRET = new TextEncoder().encode(process.env.JWT_SECRET);
export async function middleware(req: NextRequest) {
const token = req.cookies.get("token")?.value;
const url = req.nextUrl.clone();
if (req.nextUrl.pathname.startsWith("/admin") && req.nextUrl.pathname !== "/admin/login") {
if (!token) {
url.pathname = "/admin/login";
return NextResponse.redirect(url);
}
try {
const { payload } = await jwtVerify(token, JWT_SECRET);
if (payload.role !== "admin") throw new Error();
} catch {
url.pathname = "/admin/login";
return NextResponse.redirect(url);
}
}
return NextResponse.next();
}
export const config = {
matcher: ["/admin", "/admin/:path*"],
};