-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
38 lines (31 loc) · 1.02 KB
/
middleware.ts
File metadata and controls
38 lines (31 loc) · 1.02 KB
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
import { NextRequest, NextResponse } from "next/server";
import authConfig from "./auth.config"
import NextAuth from "next-auth"
export const { auth: authMiddleware } = NextAuth(authConfig)
export async function middleware(req: NextRequest) {
if ((await isAdminAuthenticated(req)) === false) {
return new NextResponse("Unauthorized", {
status: 401,
headers: { "WWW-Authenticate": "Basic" },
});
}
}
async function isAdminAuthenticated(req: NextRequest) {
const authHeader =
req.headers.get("authorization") || req.headers.get("Authorization");
if (authHeader == null) return false;
const [username, password] = Buffer.from(authHeader.split(" ")[1], "base64")
.toString()
.split(":");
if (
username === process.env.ADMIN_USERNAME &&
password === process.env.ADMIN_PASSWORD
) {
return true;
}
return false;
}
export const config = {
middleware: ["authMiddleware", "middleware"],
matcher: "/admin/:path*",
};