-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
43 lines (34 loc) · 971 Bytes
/
middleware.ts
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
33
34
35
36
37
38
39
40
41
42
43
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
const { HTTP_BASIC_AUTH } = process.env;
const [AUTH_USER, AUTH_PASS] = HTTP_BASIC_AUTH.split(":");
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) => {
if (req.method !== "GET") return true;
const authheader =
req.headers.get("authorization") || req.headers.get("Authorization");
if (!authheader) {
return false;
}
const auth = Buffer.from(authheader.split(" ")[1], "base64")
.toString()
.split(":");
const user = auth[0];
const pass = auth[1];
if (user == AUTH_USER && pass == AUTH_PASS) {
return true;
} else {
return false;
}
};
export const config = {
matcher: "/admin",
};