@plainbrew/vercel-basic-auth
v0.2.0
Published
Basic Auth handler for Vercel Edge Middleware
Readme
@plainbrew/vercel-basic-auth
Basic Auth handler for Vercel Edge Middleware.
Install
pnpm add @plainbrew/vercel-basic-authUsage
proxy.ts:
import { basicAuth } from "@plainbrew/vercel-basic-auth";
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
export default async function proxy(request: NextRequest) {
const basicAuthResponse = basicAuth(request, {
username: process.env.BASIC_AUTH_USERNAME,
password: process.env.BASIC_AUTH_PASSWORD,
// vercelEnvTarget: "all", // Apply Basic Auth to all Vercel environments
// dev: true, // Apply Basic Auth in local development
});
if (basicAuthResponse) return basicAuthResponse;
return NextResponse.next();
}Options
| Option | Type | Required | Default | Description |
| ----------------- | --------------------- | -------- | ------------------- | ------------------------------------------ |
| username | string \| undefined | ✓ | | Basic Auth username |
| password | string \| undefined | ✓ | | Basic Auth password |
| vercelEnvTarget | string | | 'only-production' | Vercel environments to apply Basic Auth |
| dev | boolean | | false | Apply Basic Auth in NODE_ENV=development |
vercelEnvTarget
| Value | Behavior |
| ----------------- | --------------------------------------------- |
| only-production | Apply Basic Auth to Vercel production only |
| all | Apply Basic Auth to all Vercel environments |
| disabled | Disable Basic Auth on all Vercel environments |
Notes
- Basic Auth is only applied on Vercel (
VERCEL=1) by default. Local development is skipped unlessdev: true. usernameandpasswordacceptundefined, soprocess.env.XXXcan be passed directly without type assertion.- If
usernameorpasswordisundefinedor an empty string when Basic Auth is actually applied, an error is thrown. This prevents accidentally bypassing Basic Auth due to missing credentials.
