svrjs-nextjs-website/middleware.ts
2024-06-20 19:08:05 +05:30

21 lines
447 B
TypeScript

import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
export function middleware(req: NextRequest) {
const url = req.nextUrl.clone();
if (url.pathname.startsWith("/admin")) {
const authCookie = req.cookies.get("auth");
if (!authCookie) {
url.pathname = "/login";
return NextResponse.redirect(url);
}
}
return NextResponse.next();
}
export const config = {
matcher: ["/admin/:path*"],
};