Skip to content

Commit

Permalink
feat: add in cors for the API auth route
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeocodes committed Apr 21, 2024
1 parent fa50b57 commit 8056fbf
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { NextResponse, type NextRequest } from "next/server";

const corsOptions: {
allowedMethods: string[];
allowedOrigins: string[];
allowedHeaders: string[];
exposedHeaders: string[];
maxAge?: number;
credentials: boolean;
} = {
allowedMethods: (process.env?.ALLOWED_METHODS || "").split(","),
allowedOrigins: (process.env?.ALLOWED_ORIGIN || "").split(","),
allowedHeaders: (process.env?.ALLOWED_HEADERS || "").split(","),
exposedHeaders: (process.env?.EXPOSED_HEADERS || "").split(","),
maxAge:
(process.env?.PREFLIGHT_MAX_AGE &&
parseInt(process.env?.PREFLIGHT_MAX_AGE)) ||
undefined, // 60 * 60 * 24 * 30, // 30 days
credentials: process.env?.CREDENTIALS == "true",
};

/**
* Middleware function that handles CORS configuration for API routes.
*
* This middleware function is responsible for setting the appropriate CORS headers
* on the response, based on the configured CORS options. It checks the origin of
* the request and sets the `Access-Control-Allow-Origin` header accordingly. It
* also sets the other CORS-related headers, such as `Access-Control-Allow-Credentials`,
* `Access-Control-Allow-Methods`, `Access-Control-Allow-Headers`, and
* `Access-Control-Expose-Headers`.
*
* The middleware function is configured to be applied to all API routes, as defined
* by the `config` object at the end of the file.
*/
export function middleware(request: NextRequest) {
// Response
const response = NextResponse.next();

// Allowed origins check
const origin = request.headers.get("origin") ?? "";
if (
corsOptions.allowedOrigins.includes("*") ||
corsOptions.allowedOrigins.includes(origin)
) {
response.headers.set("Access-Control-Allow-Origin", origin);
}

// Set default CORS headers
response.headers.set(
"Access-Control-Allow-Credentials",
corsOptions.credentials.toString()
);
response.headers.set(
"Access-Control-Allow-Methods",
corsOptions.allowedMethods.join(",")
);
response.headers.set(
"Access-Control-Allow-Headers",
corsOptions.allowedHeaders.join(",")
);
response.headers.set(
"Access-Control-Expose-Headers",
corsOptions.exposedHeaders.join(",")
);
response.headers.set(
"Access-Control-Max-Age",
corsOptions.maxAge?.toString() ?? ""
);

// Return
return response;
}

// See "Matching Paths" below to learn more
export const config = {
matcher: "/api/:path*",
};

0 comments on commit 8056fbf

Please sign in to comment.