Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/vercel-basic-auth-only-preview.md
Original file line number Diff line number Diff line change
@@ -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.
1 change: 1 addition & 0 deletions packages/vercel-basic-auth/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |

Expand Down
38 changes: 38 additions & 0 deletions packages/vercel-basic-auth/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
5 changes: 4 additions & 1 deletion packages/vercel-basic-auth/src/index.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Loading