Skip to content

Instantly share code, notes, and snippets.

@jgatjens
Created March 29, 2022 13:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgatjens/938b990c87f0e7530af52683c538a7c1 to your computer and use it in GitHub Desktop.
Save jgatjens/938b990c87f0e7530af52683c538a7c1 to your computer and use it in GitHub Desktop.
Next.js Simple Password Protection
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