(request: NextRequest)
| 13 | } |
| 14 | |
| 15 | export function middleware(request: NextRequest) { |
| 16 | const { pathname } = request.nextUrl; |
| 17 | |
| 18 | // Always allow public pages (login) |
| 19 | if (PUBLIC_ROUTES.has(pathname)) { |
| 20 | return NextResponse.next(); |
| 21 | } |
| 22 | |
| 23 | // Always allow public API routes (auth + health) |
| 24 | if (PUBLIC_API_PREFIXES.some((prefix) => pathname.startsWith(prefix))) { |
| 25 | return NextResponse.next(); |
| 26 | } |
| 27 | |
| 28 | // Check authentication |
| 29 | if (!isAuthenticated(request)) { |
| 30 | // For API routes: return 401 JSON (not a redirect) |
| 31 | if (pathname.startsWith("/api/")) { |
| 32 | return NextResponse.json( |
| 33 | { error: "Unauthorized", message: "Authentication required" }, |
| 34 | { status: 401 } |
| 35 | ); |
| 36 | } |
| 37 | |
| 38 | // For page routes: redirect to login |
| 39 | const loginUrl = new URL("/login", request.url); |
| 40 | loginUrl.searchParams.set("from", pathname); |
| 41 | return NextResponse.redirect(loginUrl); |
| 42 | } |
| 43 | |
| 44 | return NextResponse.next(); |
| 45 | } |
| 46 | |
| 47 | export const config = { |
| 48 | matcher: [ |
nothing calls this directly
no test coverage detected