[website] Add basic auth (#422)

This commit is contained in:
Pavel 2023-06-21 17:38:50 +01:00 committed by GitHub
parent 45e36b2360
commit b63515e65f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 25 additions and 0 deletions

View File

@ -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"` },
})
}