svrjs-nextjs-website/middleware.ts

22 lines
447 B
TypeScript
Raw Normal View History

2024-06-20 15:38:05 +02:00
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*"],
};