-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproxy.ts
More file actions
32 lines (27 loc) · 973 Bytes
/
proxy.ts
File metadata and controls
32 lines (27 loc) · 973 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { NextResponse, type NextRequest } from "next/server";
export async function proxy(request: NextRequest) {
const password = process.env.DASHBOARD_PASSWORD;
if (!password) return NextResponse.next();
const expectedUser = process.env.DASHBOARD_USER || "auren";
const header = request.headers.get("authorization");
if (header?.startsWith("Basic ")) {
try {
const decoded = atob(header.slice(6));
const idx = decoded.indexOf(":");
const user = idx >= 0 ? decoded.slice(0, idx) : decoded;
const pass = idx >= 0 ? decoded.slice(idx + 1) : "";
if (user === expectedUser && pass === password) {
return NextResponse.next();
}
} catch {
// fall through to challenge
}
}
return new NextResponse(null, {
status: 401,
headers: { "WWW-Authenticate": 'Basic realm="auren"' },
});
}
export const config = {
matcher: ["/((?!api/health|_next/static|_next/image|favicon.ico|.*\\..*).*)"],
};