- Заполнены заглушки: user-friendly-messages, health, aria, keyboard - backend: core/auth.py, /api/v1/stats; cached-api → backend-client при USE_MOCK_DATA=false - .env.example, middleware auth (skip при USE_MOCK_DATA), убраны неиспользуемые deps - Страницы: airworthiness, maintenance, defects, modifications; AircraftAddModal, Sidebar - Главная страница: REFLY — Контроль лётной годности (вместо Numerology App) - Линт/скрипты: eslintrc, security, cleanup, logs, api inbox/knowledge Co-authored-by: Cursor <cursoragent@cursor.com>
55 lines
1.9 KiB
TypeScript
55 lines
1.9 KiB
TypeScript
/**
|
|
* Next.js Middleware
|
|
*/
|
|
import { NextResponse } from 'next/server';
|
|
import type { NextRequest } from 'next/server';
|
|
|
|
const PUBLIC_API_ROUTES = ['/api/health', '/api/openapi'];
|
|
|
|
function isPublicRoute(pathname: string): boolean {
|
|
return PUBLIC_API_ROUTES.some(route => pathname.startsWith(route));
|
|
}
|
|
|
|
export function middleware(request: NextRequest) {
|
|
const pathname = request.nextUrl.pathname;
|
|
|
|
if (
|
|
pathname.startsWith('/_next/') ||
|
|
pathname.startsWith('/static/') ||
|
|
pathname.startsWith('/favicon.ico')
|
|
) {
|
|
return NextResponse.next();
|
|
}
|
|
|
|
const response = NextResponse.next();
|
|
|
|
if (!pathname.startsWith('/api') && !pathname.startsWith('/_next')) {
|
|
response.headers.set('X-Content-Type-Options', 'nosniff');
|
|
response.headers.set('X-Frame-Options', 'DENY');
|
|
response.headers.set('X-XSS-Protection', '1; mode=block');
|
|
response.headers.set('Referrer-Policy', 'strict-origin-when-cross-origin');
|
|
const csp = "default-src 'self'; script-src 'self' 'unsafe-eval' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' data: https:; font-src 'self'; connect-src 'self' https://api.openai.com";
|
|
response.headers.set('Content-Security-Policy', csp);
|
|
}
|
|
|
|
// AUTH: в production требуется токен. Dev: ENABLE_DEV_AUTH + NEXT_PUBLIC_DEV_TOKEN на бэкенде
|
|
const isDev = process.env.NODE_ENV === 'development';
|
|
const skipAuth = isDev || process.env.NEXT_PUBLIC_USE_MOCK_DATA === 'true';
|
|
if (pathname.startsWith('/api') && !isPublicRoute(pathname) && !skipAuth) {
|
|
const authHeader = request.headers.get('authorization');
|
|
const cookieToken = request.cookies.get('auth-token')?.value;
|
|
if (!authHeader && !cookieToken) {
|
|
return NextResponse.json(
|
|
{ error: 'Unauthorized', message: 'Missing authentication token' },
|
|
{ status: 401 }
|
|
);
|
|
}
|
|
}
|
|
|
|
return response;
|
|
}
|
|
|
|
export const config = {
|
|
matcher: ['/api/:path*', '/(.*)'],
|
|
};
|