import { NextResponse } from 'next/server'; import type { NextRequest } from 'next/server'; export function middleware(request: NextRequest) { const { pathname } = request.nextUrl; if (pathname.startsWith('/dashboard')) { const authToken = request.cookies.get('Authorization')?.value; const isPrefetch = request.headers.get('Next-Router-Prefetch') === '1' || request.headers.get('purpose') === 'prefetch'; if (!authToken) { if (isPrefetch) { // Let prefetch requests through so caching works return NextResponse.next(); } const loginUrl = new URL('/auth/login', request.url); loginUrl.searchParams.set('redirectUrl', pathname); return NextResponse.redirect(loginUrl); } } return NextResponse.next(); } export const config = { matcher: ['/dashboard/:path*'], };