svrjs-nextjs-website/middleware.ts
2024-06-27 01:17:18 +05:30

19 lines
528 B
TypeScript

import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
import { getToken } from "next-auth/jwt";
export async function middleware(req: NextRequest) {
const token = await getToken({ req, secret: process.env.NEXTAUTH_SECRET });
if (req.nextUrl.pathname.startsWith("/admin") && !token) {
const url = req.nextUrl.clone();
url.pathname = "/login";
return NextResponse.redirect(url);
}
return NextResponse.next();
}
export const config = {
matcher: ["/admin/:path*"],
};