|
| 1 | +import { NextResponse, type NextRequest } from "next/server"; |
| 2 | + |
| 3 | +const corsOptions: { |
| 4 | + allowedMethods: string[]; |
| 5 | + allowedOrigins: string[]; |
| 6 | + allowedHeaders: string[]; |
| 7 | + exposedHeaders: string[]; |
| 8 | + maxAge?: number; |
| 9 | + credentials: boolean; |
| 10 | +} = { |
| 11 | + allowedMethods: (process.env?.ALLOWED_METHODS || "").split(","), |
| 12 | + allowedOrigins: (process.env?.ALLOWED_ORIGIN || "").split(","), |
| 13 | + allowedHeaders: (process.env?.ALLOWED_HEADERS || "").split(","), |
| 14 | + exposedHeaders: (process.env?.EXPOSED_HEADERS || "").split(","), |
| 15 | + maxAge: |
| 16 | + (process.env?.PREFLIGHT_MAX_AGE && |
| 17 | + parseInt(process.env?.PREFLIGHT_MAX_AGE)) || |
| 18 | + undefined, // 60 * 60 * 24 * 30, // 30 days |
| 19 | + credentials: process.env?.CREDENTIALS == "true", |
| 20 | +}; |
| 21 | + |
| 22 | +/** |
| 23 | + * Middleware function that handles CORS configuration for API routes. |
| 24 | + * |
| 25 | + * This middleware function is responsible for setting the appropriate CORS headers |
| 26 | + * on the response, based on the configured CORS options. It checks the origin of |
| 27 | + * the request and sets the `Access-Control-Allow-Origin` header accordingly. It |
| 28 | + * also sets the other CORS-related headers, such as `Access-Control-Allow-Credentials`, |
| 29 | + * `Access-Control-Allow-Methods`, `Access-Control-Allow-Headers`, and |
| 30 | + * `Access-Control-Expose-Headers`. |
| 31 | + * |
| 32 | + * The middleware function is configured to be applied to all API routes, as defined |
| 33 | + * by the `config` object at the end of the file. |
| 34 | + */ |
| 35 | +export function middleware(request: NextRequest) { |
| 36 | + // Response |
| 37 | + const response = NextResponse.next(); |
| 38 | + |
| 39 | + // Allowed origins check |
| 40 | + const origin = request.headers.get("origin") ?? ""; |
| 41 | + if ( |
| 42 | + corsOptions.allowedOrigins.includes("*") || |
| 43 | + corsOptions.allowedOrigins.includes(origin) |
| 44 | + ) { |
| 45 | + response.headers.set("Access-Control-Allow-Origin", origin); |
| 46 | + } |
| 47 | + |
| 48 | + // Set default CORS headers |
| 49 | + response.headers.set( |
| 50 | + "Access-Control-Allow-Credentials", |
| 51 | + corsOptions.credentials.toString() |
| 52 | + ); |
| 53 | + response.headers.set( |
| 54 | + "Access-Control-Allow-Methods", |
| 55 | + corsOptions.allowedMethods.join(",") |
| 56 | + ); |
| 57 | + response.headers.set( |
| 58 | + "Access-Control-Allow-Headers", |
| 59 | + corsOptions.allowedHeaders.join(",") |
| 60 | + ); |
| 61 | + response.headers.set( |
| 62 | + "Access-Control-Expose-Headers", |
| 63 | + corsOptions.exposedHeaders.join(",") |
| 64 | + ); |
| 65 | + response.headers.set( |
| 66 | + "Access-Control-Max-Age", |
| 67 | + corsOptions.maxAge?.toString() ?? "" |
| 68 | + ); |
| 69 | + |
| 70 | + // Return |
| 71 | + return response; |
| 72 | +} |
| 73 | + |
| 74 | +// See "Matching Paths" below to learn more |
| 75 | +export const config = { |
| 76 | + matcher: "/api/:path*", |
| 77 | +}; |
0 commit comments