-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
37 lines (30 loc) · 1.08 KB
/
middleware.ts
File metadata and controls
37 lines (30 loc) · 1.08 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
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
export function middleware(request: NextRequest) {
const response = NextResponse.next()
// Handle dashboard routes
if (request.nextUrl.pathname.startsWith('/dashboard')) {
// Check for authentication token
const token = request.cookies.get('next-auth.session-token')?.value
if (!token) {
// Redirect to sign-in if not authenticated
return NextResponse.redirect(new URL('/signin', request.url))
}
// Add headers to prevent RSC payload caching issues
response.headers.set('Cache-Control', 'no-cache, no-store, must-revalidate')
response.headers.set('Pragma', 'no-cache')
response.headers.set('Expires', '0')
}
// Handle API routes
if (request.nextUrl.pathname.startsWith('/api/')) {
response.headers.set('Cache-Control', 'no-cache, no-store, must-revalidate')
response.headers.set('Access-Control-Allow-Credentials', 'true')
}
return response
}
export const config = {
matcher: [
'/dashboard/:path*',
'/api/:path*'
]
}