From b89002426936f1631a3a55b96481230ad85661a3 Mon Sep 17 00:00:00 2001 From: Wataru Ono Date: Wed, 9 Feb 2022 22:14:30 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20Basic=20=E8=AA=8D=E8=A8=BC=E3=81=AE?= =?UTF-8?q?=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/_middleware.ts | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 src/pages/_middleware.ts diff --git a/src/pages/_middleware.ts b/src/pages/_middleware.ts new file mode 100644 index 0000000..c693793 --- /dev/null +++ b/src/pages/_middleware.ts @@ -0,0 +1,21 @@ +import { NextRequest, NextResponse } from "next/server"; + +export const middleware = (req: NextRequest) => { + const basicAuth = req.headers.get("authorization"); + + if (basicAuth) { + const auth = basicAuth.split(" ")[1]; + const [user, password] = Buffer.from(auth, "base64").toString().split(":"); + + if (user === process.env.BASIC_AUTH_USER && password === process.env.BASIC_AUTH_PASSWORD) { + return NextResponse.next(); + } + } + + return new Response("Auth required", { + status: 401, + headers: { + "WWW-Authenticate": "Basic realm='Secure Area'", + } + }); +};