Created
March 29, 2022 13:48
-
-
Save jgatjens/938b990c87f0e7530af52683c538a7c1 to your computer and use it in GitHub Desktop.
Next.js Simple Password Protection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { NextRequest, NextResponse } from 'next/server' | |
export function middleware(req: NextRequest) { | |
const basicAuth = req.headers.get('authorization') | |
if (basicAuth) { | |
const auth = basicAuth.split(' ')[1] | |
const [user, pwd] = Buffer.from(auth, 'base64').toString().split(':') | |
if (user === '4dmin' && pwd === 'testpwd123') { | |
return NextResponse.next() | |
} | |
} | |
return new Response('Auth required', { | |
status: 401, | |
headers: { | |
'WWW-Authenticate': 'Basic realm="Secure Area"', | |
}, | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment