import { NextResponse } from 'next/server'; import type { NextRequest } from 'next/server'; export function middleware(request: NextRequest) { const token = request.cookies.get('auth_token')?.value; const isLoginPage = request.nextUrl.pathname === '/login'; // If no token exists and user is trying to access a protected route // Kick them to the login page immediately at the edge. if (!token && !isLoginPage) { const loginUrl = new URL('/login', request.url); return NextResponse.redirect(loginUrl); } // If token exists and they are hitting the login page, redirect to dashboard if (token && isLoginPage) { const dashboardUrl = new URL('/', request.url); return NextResponse.redirect(dashboardUrl); } return NextResponse.next(); } // See "Matching Paths" below to learn more export const config = { matcher: [ /* * Match all request paths except for the ones starting with: * - api (API routes) * - _next/static (static files) * - _next/image (image optimization files) * - favicon.ico (favicon file) * - images/ (public assets) * - banner.png (public assets) */ '/((?!api|_next/static|_next/image|favicon.ico|images|banner).*)', ], };