-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.tsx
29 lines (22 loc) · 984 Bytes
/
middleware.tsx
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
import {NextResponse} from "next/server"
import type {NextRequest} from "next/server"
export const middleware = (req: NextRequest) => {
if (!isAuthenticated(req)) {
return new NextResponse("Authentication required", {
status: 401,
headers: {"WWW-Authenticate": "Basic"},
})
}
return NextResponse.next()
}
const isAuthenticated = (req: NextRequest) => {
const authHeader = req.headers.get("authorization") || req.headers.get("Authorization")
if (!authHeader) return false
const [user, pass] = Buffer.from(authHeader.split(" ")[1], "base64").toString().split(":")
const acceptedCredentials = process.env.HTTP_BASIC_AUTH?.split("|").map(cred => cred.split(":")) || []
return !!acceptedCredentials.find(creds => req.nextUrl.pathname.indexOf(`/${creds[0]}/`) > 0 && creds[1] === user && creds[2] === pass)
}
// Step 3. Configure "Matching Paths" below to protect routes with HTTP Basic Auth
export const config = {
matcher: "/system/:path*",
}