svrjs-nextjs-website/middleware.ts

29 lines
690 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";
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) {
2024-07-25 22:09:06 +02:00
const token = await getToken({ req, secret: process.env.NEXTAUTH_SECRET });
2024-06-20 15:38:05 +02:00
2024-07-25 22:09:06 +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-07-25 22:09:06 +02:00
return NextResponse.next();
2024-06-20 15:38:05 +02:00
}
export const config = {
2024-07-25 22:09:06 +02:00
matcher: [
"/admin/:path*",
"/api/delete/downloads/[id]",
"/api/delete/logs/[id]",
"/api/delete/mods/[id]",
"/api/upload",
"/api/uploadlogs",
"/api/uploadmods",
"/api/uploadthing",
],
2024-06-20 15:38:05 +02:00
};