-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
39 lines (33 loc) · 1.14 KB
/
Copy pathmiddleware.ts
File metadata and controls
39 lines (33 loc) · 1.14 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
import { clerkMiddleware } from "@clerk/nextjs/server";
import { NextResponse } from "next/server";
const corsHeaders = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type, Authorization",
};
export default clerkMiddleware((auth, request) => {
// Handle preflight OPTIONS requests first
if (request.method === "OPTIONS") {
return new NextResponse(null, {
status: 200,
headers: corsHeaders,
});
}
// Let Clerk handle the request and get the response
const response = NextResponse.next();
// Add CORS headers to all API responses
if (request.nextUrl.pathname.startsWith("/api/")) {
Object.entries(corsHeaders).forEach(([key, value]) => {
response.headers.set(key, value);
});
}
return response;
});
export const config = {
matcher: [
// Skip Next.js internals and all static files, unless found in search params
"/((?!_next|[^?]*\\.(?:html?|css|js(?!on)|jpe?g|webp|png|gif|svg|ttf|woff2?|ico|csv|docx?|xlsx?|zip|webmanifest)).*)",
// Always run for API routes
"/(api|trpc)(.*)",
],
};