|
|
|
import { NextResponse } from 'next/server'; |
|
import type { NextRequest } from 'next/server'; |
|
|
|
|
|
const publicRoutes = ['/api/img/', '/api/files/']; |
|
|
|
export function middleware(request: NextRequest) { |
|
|
|
|
|
const tokenToUse = process.env.AI_TOOLKIT_AUTH || null; |
|
if (!tokenToUse) { |
|
return NextResponse.next(); |
|
} |
|
|
|
|
|
const token = request.headers.get('Authorization')?.split(' ')[1]; |
|
|
|
|
|
if (publicRoutes.some(route => request.nextUrl.pathname.startsWith(route))) { |
|
return NextResponse.next(); |
|
} |
|
|
|
|
|
|
|
if (request.nextUrl.pathname.startsWith('/api/')) { |
|
if (!token || token !== tokenToUse) { |
|
|
|
return new NextResponse(JSON.stringify({ error: 'Unauthorized' }), { |
|
status: 401, |
|
headers: { 'Content-Type': 'application/json' }, |
|
}); |
|
} |
|
|
|
|
|
return NextResponse.next(); |
|
} |
|
|
|
|
|
return NextResponse.next(); |
|
} |
|
|
|
|
|
export const config = { |
|
matcher: [ |
|
|
|
'/api/:path*', |
|
], |
|
}; |
|
|