echos-ocr/src/middleware.ts

46 lines
901 B
TypeScript
Raw Normal View History

import { auth } from "@/auth";
import { NextResponse } from "next/server";
const publicPaths = [
"/login",
"/signup",
"/setup",
"/api/auth",
"/api/health",
"/api/setup",
];
function isPublic(pathname: string) {
return publicPaths.some(
(p) => pathname === p || pathname.startsWith(p + "/")
);
}
export default auth((req) => {
const { pathname } = req.nextUrl;
if (
pathname.startsWith("/_next") ||
pathname.startsWith("/favicon") ||
pathname.includes(".")
) {
return NextResponse.next();
}
if (isPublic(pathname)) {
return NextResponse.next();
}
if (!req.auth) {
const loginUrl = new URL("/login", req.url);
loginUrl.searchParams.set("callbackUrl", pathname);
return NextResponse.redirect(loginUrl);
}
return NextResponse.next();
});
export const config = {
matcher: ["/((?!_next/static|_next/image|favicon.ico).*)"],
};