Skip to content

Commit

Permalink
feat: Basic 認証の実装
Browse files Browse the repository at this point in the history
  • Loading branch information
watagit committed Feb 9, 2022
1 parent ced4db6 commit b890024
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/pages/_middleware.ts
Original file line number Diff line number Diff line change
@@ -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'",
}
});
};

0 comments on commit b890024

Please sign in to comment.