31 lines
1.2 KiB
TypeScript
31 lines
1.2 KiB
TypeScript
import { NextResponse, type NextRequest } from "next/server";
|
|
import { SESSION_COOKIE_NAME, verifySessionToken } from "@/lib/session";
|
|
|
|
// Next.js 16 renamed the `middleware.ts` file convention to `proxy.ts`
|
|
// (https://nextjs.org/docs/messages/middleware-to-proxy) — it now always
|
|
// runs on the Node.js runtime, which is why plain `node:crypto` in
|
|
// session.ts works fine here without any Edge-runtime workaround.
|
|
|
|
const PUBLIC_PATHS = ["/login"];
|
|
|
|
export default function proxy(request: NextRequest) {
|
|
const { pathname } = request.nextUrl;
|
|
if (PUBLIC_PATHS.some((path) => pathname.startsWith(path))) return NextResponse.next();
|
|
|
|
const token = request.cookies.get(SESSION_COOKIE_NAME)?.value;
|
|
const session = verifySessionToken(token);
|
|
if (!session) {
|
|
const loginUrl = new URL("/login", request.url);
|
|
return NextResponse.redirect(loginUrl);
|
|
}
|
|
|
|
return NextResponse.next();
|
|
}
|
|
|
|
export const config = {
|
|
// Runs on every page and API route except static assets — this app has
|
|
// no public-facing content at all, unlike the main app's proxy.ts,
|
|
// which has to carve out installation/customer-facing exceptions.
|
|
matcher: ["/((?!_next/static|_next/image|favicon.ico).*)"],
|
|
};
|