mercury-web/src/middleware.ts

15 lines
754 B
TypeScript

import { NextRequest, NextResponse } from "next/server";
import { COOKIE } from "./server/session";
const PROTECTED = ["/home", "/accounting", "/planner", "/assets", "/profile"];
export function middleware(req: NextRequest) {
const { pathname } = req.nextUrl;
const hasSession = req.cookies.has(COOKIE);
if (PROTECTED.some((p) => pathname.startsWith(p)) && !hasSession) {
return NextResponse.redirect(new URL("/login", req.url));
}
if ((pathname === "/login" || pathname === "/") && hasSession) {
return NextResponse.redirect(new URL("/home", req.url));
}
return NextResponse.next();
}
export const config = { matcher: ["/", "/login", "/home/:path*", "/accounting/:path*", "/planner/:path*", "/assets/:path*", "/profile/:path*"] };