-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
47 lines (39 loc) · 1.31 KB
/
middleware.ts
File metadata and controls
47 lines (39 loc) · 1.31 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
40
41
42
43
44
45
46
47
import { NextRequest, NextResponse } from 'next/server';
import { logger } from './lib/logger';
import { getClientIP } from './lib/utils';
export function middleware(request: NextRequest) {
const startTime = Date.now();
const { pathname } = request.nextUrl;
// Log request
logger.info(`Request: ${request.method} ${pathname}`, {
ip: getClientIP(request),
userAgent: request.headers.get('user-agent'),
});
// Add security headers
const response = NextResponse.next();
// Security headers
response.headers.set('X-Content-Type-Options', 'nosniff');
response.headers.set('X-Frame-Options', 'DENY');
response.headers.set('X-XSS-Protection', '1; mode=block');
response.headers.set('Referrer-Policy', 'strict-origin-when-cross-origin');
response.headers.set(
'Permissions-Policy',
'camera=(), microphone=(), geolocation=()'
);
// CORS headers
response.headers.set('Access-Control-Allow-Origin', '*');
response.headers.set(
'Access-Control-Allow-Methods',
'GET, POST, PUT, DELETE, OPTIONS'
);
response.headers.set(
'Access-Control-Allow-Headers',
'Content-Type, Authorization, X-API-Key'
);
// Add response time header
response.headers.set('X-Response-Time', `${Date.now() - startTime}ms`);
return response;
}
export const config = {
matcher: ['/api/:path*'],
};