-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
28 lines (24 loc) · 1 KB
/
middleware.ts
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
import { NextRequest, NextResponse } from 'next/server';
// Custom CORS middleware
async function corsMiddleware(req: NextRequest, res: NextResponse) {
const allowedMethods = ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'];
const allowedHeaders = ['Content-Type', 'Authorization'];
if (req.method === 'OPTIONS') {
res.headers.set('Access-Control-Allow-Methods', allowedMethods.join(','));
res.headers.set('Access-Control-Allow-Headers', allowedHeaders.join(','));
res.headers.set('Access-Control-Allow-Origin', '*');
return new NextResponse(null, { status: 204 });
} else {
res.headers.set('Access-Control-Allow-Origin', '*');
}
}
// Middleware function to handle requests
export async function middleware(req: NextRequest) {
const response = NextResponse.next();
await corsMiddleware(req, response);
return response;
}
// Apply the middleware only to API routes
export const config = {
matcher: '/api/:path*', // Apply middleware to all API routes
};