|
1 | | -import { NextRequest, NextResponse } from "next/server"; |
| 1 | +import { NextResponse } from "next/server"; |
2 | 2 |
|
3 | 3 | import { NextRequestWithAuth, withAuth } from "next-auth/middleware"; |
4 | 4 |
|
5 | 5 | import { isPrivatePath } from "@/lib/utils"; |
6 | 6 |
|
7 | 7 | export default function middleware(req: NextRequestWithAuth) { |
8 | | - // HTTP Basic Auth logic |
9 | | - function isAuthenticated(req: NextRequest) { |
10 | | - // Skip auth if disabled via environment config |
11 | | - const isBasicAuthEnabled = |
12 | | - (process.env.BASIC_AUTH_ENABLED ?? "").toLowerCase() === "true"; |
13 | | - if (!isBasicAuthEnabled) return true; |
14 | | - |
15 | | - const authHeader = |
16 | | - req.headers.get("authorization") || req.headers.get("Authorization"); |
17 | | - if (!authHeader?.startsWith("Basic ")) return false; |
18 | | - |
19 | | - const [user, pass] = Buffer.from(authHeader.split(" ")[1], "base64") |
20 | | - .toString() |
21 | | - .split(":"); |
22 | | - |
23 | | - return ( |
24 | | - user === process.env.BASIC_AUTH_USER && |
25 | | - pass === process.env.BASIC_AUTH_PASSWORD |
26 | | - ); |
27 | | - } |
28 | | - |
29 | | - const PUBLIC_FILE = /\.(.*)$/; |
30 | | - const BASIC_AUTH_EXCLUDED_PATHS = [/^\/health\/?$/]; |
31 | | - |
32 | | - if ( |
33 | | - PUBLIC_FILE.test(req.nextUrl.pathname) || |
34 | | - BASIC_AUTH_EXCLUDED_PATHS.some((pattern) => |
35 | | - pattern.test(req.nextUrl.pathname), |
36 | | - ) |
37 | | - ) { |
38 | | - return NextResponse.next(); |
39 | | - } |
40 | | - |
41 | | - if (!isAuthenticated(req)) { |
42 | | - return new NextResponse("Authentication required", { |
43 | | - status: 401, |
44 | | - headers: { "WWW-Authenticate": "Basic" }, |
45 | | - }); |
46 | | - } |
47 | | - |
48 | 8 | if (isPrivatePath(req.nextUrl.pathname)) { |
49 | 9 | return withAuth(req, { |
50 | 10 | pages: { |
|
0 commit comments