import { NextResponse } from 'next/server'; import type { NextRequest } from 'next/server'; export const config = { matcher: [ '/((?!api|_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)', ], }; export function middleware(req: NextRequest) { const url = req.nextUrl; const hostname = req.headers.get('host') || ''; // Determine which subdomain we are on const isAdmin = hostname.includes('admin.armoreg.com') || hostname.startsWith('admin.localhost'); const isPanel = hostname.includes('panel.armoreg.com') || hostname.startsWith('panel.localhost'); // Simple authentication check via cookie (to be expanded later) const isAuthenticated = req.cookies.has('auth_token'); const isLoginPage = url.pathname === '/login'; // If not authenticated and not on login page, redirect to login if (!isAuthenticated && !isLoginPage) { return NextResponse.redirect(new URL('/login', req.url)); } // If authenticated and on login page, redirect to root if (isAuthenticated && isLoginPage) { return NextResponse.redirect(new URL('/', req.url)); } // Rewrite to the appropriate folder inside /app if (isAdmin) { return NextResponse.rewrite(new URL(`/admin${url.pathname}`, req.url)); } if (isPanel) { return NextResponse.rewrite(new URL(`/panel${url.pathname}`, req.url)); } return NextResponse.next(); }