File size: 844 Bytes
6ab8437
 
 
822d72c
4435002
 
 
6ab8437
 
f2f7617
6ab8437
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";

export function proxy(request: NextRequest) {
    // Only protect /api routes, but exclude status and health for polling
    if (request.nextUrl.pathname.startsWith("/api") &&
        !request.nextUrl.pathname.includes("/api/v1/health")) {
        const apiKey = request.headers.get("x-api-key");
        const validApiKey = process.env.API_KEY;

        // Check if API key matches (or if environment variable is not set, fail safe)
        if (!validApiKey || apiKey !== validApiKey) {
            return NextResponse.json(
                { error: "Unauthorized: Invalid or missing API Key" },
                { status: 401 }
            );
        }
    }

    return NextResponse.next();
}

export const config = {
    matcher: "/api/:path*",
};