2024-06-20 15:38:05 +02:00
|
|
|
import { NextResponse } from "next/server";
|
|
|
|
import type { NextRequest } from "next/server";
|
2024-06-26 21:47:18 +02:00
|
|
|
import { getToken } from "next-auth/jwt";
|
2024-06-20 15:38:05 +02:00
|
|
|
|
2024-06-26 21:47:18 +02:00
|
|
|
export async function middleware(req: NextRequest) {
|
|
|
|
const token = await getToken({ req, secret: process.env.NEXTAUTH_SECRET });
|
2024-06-20 15:38:05 +02:00
|
|
|
|
2024-06-26 21:47:18 +02:00
|
|
|
if (req.nextUrl.pathname.startsWith("/admin") && !token) {
|
|
|
|
const url = req.nextUrl.clone();
|
|
|
|
url.pathname = "/login";
|
|
|
|
return NextResponse.redirect(url);
|
|
|
|
}
|
2024-06-20 15:38:05 +02:00
|
|
|
|
2024-06-26 21:47:18 +02:00
|
|
|
return NextResponse.next();
|
2024-06-20 15:38:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export const config = {
|
2024-06-26 21:47:18 +02:00
|
|
|
matcher: ["/admin/:path*"],
|
2024-06-20 15:38:05 +02:00
|
|
|
};
|