From b63515e65f583fa25da93591554286474d5864cb Mon Sep 17 00:00:00 2001 From: Pavel <14926950+prichodko@users.noreply.github.com> Date: Wed, 21 Jun 2023 17:38:50 +0100 Subject: [PATCH] [website] Add basic auth (#422) --- apps/website/src/middleware.ts | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 apps/website/src/middleware.ts diff --git a/apps/website/src/middleware.ts b/apps/website/src/middleware.ts new file mode 100644 index 00000000..938cb8eb --- /dev/null +++ b/apps/website/src/middleware.ts @@ -0,0 +1,25 @@ +import { NextResponse } from 'next/server' + +import type { NextRequest } from 'next/server' + +export function middleware(req: NextRequest) { + const basicAuth = req.headers.get('authorization') + + if (process.env.VERCEL_ENV !== 'production') { + return NextResponse.next() + } + + if (basicAuth) { + const auth = basicAuth.split(' ')[1] + const [user, password] = Buffer.from(auth, 'base64').toString().split(':') + + if (user === 'status' && password === process.env.AUTH_PASSWORD) { + return NextResponse.next() + } + } + + return new NextResponse('Authentication Required', { + status: 401, + headers: { 'WWW-Authenticate': `Basic realm="website"` }, + }) +}