diff --git a/.changeset/vercel-basic-auth-only-preview.md b/.changeset/vercel-basic-auth-only-preview.md new file mode 100644 index 0000000..6c25294 --- /dev/null +++ b/.changeset/vercel-basic-auth-only-preview.md @@ -0,0 +1,9 @@ +--- +"@plainbrew/vercel-basic-auth": minor +--- + +feat: add `only-preview` value to `vercelEnvTarget` + +`vercelEnvTarget: "only-preview"` applies Basic Auth only on Vercel preview +deployments. Useful when you want to protect preview URLs while keeping +production open. diff --git a/packages/vercel-basic-auth/README.md b/packages/vercel-basic-auth/README.md index 69f9453..3466839 100644 --- a/packages/vercel-basic-auth/README.md +++ b/packages/vercel-basic-auth/README.md @@ -44,6 +44,7 @@ export default async function proxy(request: NextRequest) { | Value | Behavior | | ----------------- | --------------------------------------------- | | `only-production` | Apply Basic Auth to Vercel production only | +| `only-preview` | Apply Basic Auth to Vercel preview only | | `all` | Apply Basic Auth to all Vercel environments | | `disabled` | Disable Basic Auth on all Vercel environments | diff --git a/packages/vercel-basic-auth/src/index.test.ts b/packages/vercel-basic-auth/src/index.test.ts index 5e3e6d5..feec650 100644 --- a/packages/vercel-basic-auth/src/index.test.ts +++ b/packages/vercel-basic-auth/src/index.test.ts @@ -73,6 +73,44 @@ describe("Vercel 環境 (VERCEL=1)", () => { expect(res?.status).toBe(401); }); + test("vercelEnvTarget=only-preview で VERCEL_ENV=preview のとき認証チェックをする", () => { + process.env.VERCEL = "1"; + process.env.VERCEL_ENV = "preview"; + const req = makeRequest(); + const res = basicAuth(req, { + username: USERNAME, + password: PASSWORD, + vercelEnvTarget: "only-preview", + }); + expect(res?.status).toBe(401); + }); + + test("vercelEnvTarget=only-preview で VERCEL_ENV=production のとき null を返す", () => { + process.env.VERCEL = "1"; + process.env.VERCEL_ENV = "production"; + const req = makeRequest(); + expect( + basicAuth(req, { + username: USERNAME, + password: PASSWORD, + vercelEnvTarget: "only-preview", + }), + ).toBeNull(); + }); + + test("vercelEnvTarget=only-preview で VERCEL_ENV=production のとき username が undefined でも null を返す", () => { + process.env.VERCEL = "1"; + process.env.VERCEL_ENV = "production"; + const req = makeRequest(); + expect( + basicAuth(req, { + username: undefined, + password: undefined, + vercelEnvTarget: "only-preview", + }), + ).toBeNull(); + }); + test("vercelEnvTarget=all で VERCEL_ENV=preview のとき認証チェックをする", () => { process.env.VERCEL = "1"; process.env.VERCEL_ENV = "preview"; diff --git a/packages/vercel-basic-auth/src/index.ts b/packages/vercel-basic-auth/src/index.ts index 1d26aff..9a4d275 100644 --- a/packages/vercel-basic-auth/src/index.ts +++ b/packages/vercel-basic-auth/src/index.ts @@ -1,4 +1,4 @@ -export type VercelEnvTarget = "only-production" | "all" | "disabled"; +export type VercelEnvTarget = "only-production" | "only-preview" | "all" | "disabled"; export type BasicAuthOptions = { username: string | undefined; @@ -46,6 +46,9 @@ export function basicAuth( if (vercelEnvTarget === "only-production" && process.env.VERCEL_ENV !== "production") { return null; } + if (vercelEnvTarget === "only-preview" && process.env.VERCEL_ENV !== "preview") { + return null; + } } if (!authUsername || !authPassword) {