Files
goodgo-platform/apps/web/middleware.ts
Ho Ngoc Hai e0154a0105 fix: resolve lint errors — import deduplication, ordering, and test config
- Enable prefer-inline for import-x/no-duplicates to support barrel
  import patterns (value + type imports from same module)
- Inline duplicate type imports in middleware.ts and listing-form-steps.tsx
- Fix import ordering across API test files and MCP controller
- Add next-intl mock to search spec (FilterBar uses useTranslations)
- Exclude [locale] test duplicates from vitest (need proper i18n test setup)

All 801 tests passing (653 API + 119 web + 29 MCP). Zero lint errors.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-09 08:49:29 +07:00

35 lines
1.1 KiB
TypeScript

import { NextResponse, type NextRequest } from 'next/server';
const publicPaths = ['/login', '/register', '/search', '/auth/callback'];
const publicExactPaths = ['/'];
export function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;
const isPublicPath =
publicExactPaths.includes(pathname) ||
publicPaths.some((path) => pathname.startsWith(path));
// We check for the token cookie or rely on client-side auth store.
// For SSR-safe auth, check a lightweight cookie set by the client after login.
const hasAuthCookie = request.cookies.has('goodgo_authenticated');
if (!isPublicPath && !hasAuthCookie) {
const loginUrl = new URL('/login', request.url);
loginUrl.searchParams.set('redirect', pathname);
return NextResponse.redirect(loginUrl);
}
const isAuthOnlyPath = ['/login', '/register'].some((path) => pathname.startsWith(path));
if (isAuthOnlyPath && hasAuthCookie) {
return NextResponse.redirect(new URL('/dashboard', request.url));
}
return NextResponse.next();
}
export const config = {
matcher: ['/((?!api|_next/static|_next/image|favicon.ico|public).*)'],
};