Files
ai-taxonomy-literature-anal…/middleware.ts
T

24 lines
1.3 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server';
import { COOKIE, validSession } from './lib/editor-session';
export async function middleware(req: NextRequest) {
const path = req.nextUrl.pathname;
if (path === '/editor/login' || path === '/api/editor/login') return NextResponse.next();
const publicPage = path === '/' || path === '/contents' || /^\/contents\/(lean-theorem-proving|tools-of-agents)$/.test(path);
if (publicPage) return NextResponse.next();
if (!await validSession(req.cookies.get(COOKIE)?.value)) {
if (path.startsWith('/api/')) return NextResponse.json({ error: 'Editor authentication required' }, { status: 401 });
if (!path.startsWith('/editor')) return new NextResponse('Not found', { status: 404 });
const url = req.nextUrl.clone(); url.pathname = '/editor/login'; url.search = '';
return NextResponse.redirect(url);
}
if (req.method !== 'GET' && req.method !== 'HEAD' && req.headers.get('origin') !== (process.env.EDITOR_ORIGIN || req.nextUrl.origin)) {
return NextResponse.json({ error: 'Invalid request origin' }, { status: 403 });
}
const response = NextResponse.next();
response.headers.set('Cache-Control', 'private, no-store');
response.headers.set('X-Robots-Tag', 'noindex, nofollow');
return response;
}
export const config = { matcher: ['/((?!_next/static|_next/image|favicon.ico).*)'] };