-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
77 lines (66 loc) · 2.39 KB
/
middleware.ts
File metadata and controls
77 lines (66 loc) · 2.39 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// middleware.ts
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
import { createSession } from './src/app/lib/auth'
// Helper to check if a request is for the API
const isApiRoute = (pathname: string) => pathname.startsWith('/api');
// Helper to get domain from request
function getDomain(req: NextRequest) {
const host = req.headers.get('host') || '';
const protocol = process.env.NODE_ENV === 'production' ? 'https' : 'http';
return `${protocol}://${host}`;
}
// Helper to set session token in response
function setSessionToken(response: NextResponse, token: string, domain: string) {
response.cookies.set({
name: 'session-token',
value: token,
httpOnly: true,
secure: process.env.NODE_ENV === 'production',
sameSite: 'lax',
path: '/',
domain: new URL(domain).hostname,
// Set max age to 24 hours
maxAge: 24 * 60 * 60
});
response.headers.set('Authorization', `Bearer ${token}`);
}
export async function middleware(request: NextRequest) {
try {
const response = NextResponse.next();
// Add security headers
const headers = {
'X-Frame-Options': 'DENY',
'X-Content-Type-Options': 'nosniff',
'Referrer-Policy': 'origin-when-cross-origin',
'Strict-Transport-Security': 'max-age=31536000; includeSubDomains',
'X-Permitted-Cross-Domain-Policies': 'none',
'X-DNS-Prefetch-Control': 'off',
};
Object.entries(headers).forEach(([key, value]) => {
response.headers.set(key, value);
});
// Only handle session for API routes
if (isApiRoute(request.nextUrl.pathname)) {
// Get existing session token
const existingToken = request.cookies.get('session-token');
const domain = getDomain(request);
if (!existingToken) {
console.log('Creating new session token');
const token = await createSession();
setSessionToken(response, token, domain);
}
}
return response;
} catch (error) {
console.error('Middleware error:', error);
return NextResponse.next();
}
}
// Configure middleware matching
export const config = {
matcher: [
'/api/:path*',
'/((?!_next/static|_next/image|favicon.ico).*)',
],
};