fix(auth): allow same-origin browser requests through API middleware
This commit is contained in:
parent
d6b980a3ca
commit
3448c6febd
1 changed files with 9 additions and 0 deletions
|
|
@ -21,6 +21,15 @@ export function proxy(request: NextRequest) {
|
||||||
|
|
||||||
const requestApiKey = request.headers.get("X-API-Key");
|
const requestApiKey = request.headers.get("X-API-Key");
|
||||||
|
|
||||||
|
// Allow same-origin browser requests (UI -> /api/*) without exposing API_KEY to client JS.
|
||||||
|
// Keep API key auth for non-browser/external clients.
|
||||||
|
const fetchSite = request.headers.get("sec-fetch-site");
|
||||||
|
const isSameOriginBrowserRequest = fetchSite === "same-origin";
|
||||||
|
|
||||||
|
if (isSameOriginBrowserRequest) {
|
||||||
|
return NextResponse.next();
|
||||||
|
}
|
||||||
|
|
||||||
if (!requestApiKey || requestApiKey !== apiKey) {
|
if (!requestApiKey || requestApiKey !== apiKey) {
|
||||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue