Files
2026-06-06 00:49:07 +08:00

26 lines
760 B
TypeScript

import { auth } from "@/lib/auth";
import { NextResponse } from "next/server";
export default auth((req) => {
const { nextUrl } = req;
const isLoggedIn = !!req.auth;
const isAdminRoute = nextUrl.pathname.startsWith("/admin");
const isLoginPage = nextUrl.pathname === "/admin/login";
// Only the admin login page is public. Everything else under /admin
// requires a NextAuth session.
if (isAdminRoute && !isLoggedIn && !isLoginPage) {
return NextResponse.redirect(new URL("/admin/login", nextUrl));
}
if (isLoggedIn && isLoginPage) {
return NextResponse.redirect(new URL("/admin/dashboard", nextUrl));
}
return NextResponse.next();
});
export const config = {
matcher: ["/admin/:path*"],
};