// D3RO Voice — Industrial Grade Admin Route & Security Guard import { NextResponse, type NextRequest } from 'next/server' import { verifyEdgeSession } from '@/lib/edge-session' const PUBLIC_PATHS = [ '/login', '/auth/callback', '/api/auth/login', '/api/auth/logout', '/favicon.ico', '/robots.txt' ] export async function proxy(request: NextRequest): Promise { const { pathname } = request.nextUrl const isPublic = PUBLIC_PATHS.some((path) => pathname === path || pathname.startsWith(path + '/')) const isStatic = pathname.startsWith('/_next') || pathname.startsWith('/static') || pathname.includes('.') const sessionCookie = request.cookies.get('d3ro_admin_session')?.value const isAuthenticated = sessionCookie ? (await verifyEdgeSession(sessionCookie)) !== null : false if (!isAuthenticated && !isPublic && !isStatic) { const loginUrl = new URL('/login', request.url) if (pathname !== '/') loginUrl.searchParams.set('redirect', pathname) const redirectResponse = NextResponse.redirect(loginUrl) addSecurityHeaders(redirectResponse) return redirectResponse } if (isAuthenticated && pathname === '/login') { const redirectResponse = NextResponse.redirect(new URL('/', request.url)) addSecurityHeaders(redirectResponse) return redirectResponse } const response = NextResponse.next({ request: { headers: request.headers } }) addSecurityHeaders(response) return response } function addSecurityHeaders(response: NextResponse): void { response.headers.set('X-Robots-Tag', 'noindex, nofollow, noarchive, nosnippet, noimageindex') response.headers.set('X-Frame-Options', 'DENY') response.headers.set('X-Content-Type-Options', 'nosniff') response.headers.set('Referrer-Policy', 'strict-origin-when-cross-origin') response.headers.set('Permissions-Policy', 'camera=(), microphone=(), geolocation=()') } export const config = { matcher: ['/((?!_next/static|_next/image|favicon.ico).*)'] }