Skip to content

feat: vercel-basic-auth に beforeAuth / afterAuth コールバックを追加#23

Draft
amotarao wants to merge 1 commit into
mainfrom
feature/vercel-basic-auth-callbacks
Draft

feat: vercel-basic-auth に beforeAuth / afterAuth コールバックを追加#23
amotarao wants to merge 1 commit into
mainfrom
feature/vercel-basic-auth-callbacks

Conversation

@amotarao
Copy link
Copy Markdown
Member

@amotarao amotarao commented Mar 29, 2026

Summary

  • basicAuthbeforeAuth / afterAuth コールバックオプションを追加
  • beforeAuth(request): boolean — 環境判定より先に実行。false を返すと即 401、true で通常の認証フローに進む
  • afterAuth(request): boolean — 認証成功後に実行。false を返すと 401、true で通過

Test plan

  • beforeAuthfalse のとき 401 を返すこと
  • beforeAuthtrue のとき通常の認証フローに進むこと
  • NODE_ENV=development でも beforeAuth が呼ばれること
  • afterAuthfalse のとき 401 を返すこと
  • 認証失敗のとき afterAuth が呼ばれないこと

Closes #21

🤖 Generated with Claude Code

Summary by CodeRabbit

リリースノート

  • 新機能
    • basicAuth API に認証前後で実行されるコールバック機能を追加。beforeAuth で認証前のカスタム処理、afterAuth で認証後のカスタム処理が可能になり、どちらかが false を返すと 401 レスポンスとなります。

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Mar 29, 2026

Walkthrough

@plainbrew/vercel-basic-authパッケージにbeforeAuthおよびafterAuthコールバックフックを追加しました。これらのオプショナルフックを使用して、認証前および認証後のカスタム判定処理が可能になります。実装、テスト、変更セットエントリが含まれています。

Changes

Cohort / File(s) Summary
Changeset Entry
.changeset/vercel-basic-auth-callbacks.md
@plainbrew/vercel-basic-authのマイナーバージョンリリースを記録。basicAuth APIがbeforeAuthafterAuthコールバックオプションをサポートするようになったことを示します。
Implementation
packages/vercel-basic-auth/src/index.ts
BasicAuthOptions型にbeforeAuthafterAuthオプショナルプロパティを追加。beforeAuthは認可ヘッダーチェック前に実行され、falseを返すと401で即座に終了します。afterAuthは認証成功後に実行され、falseを返す場合も同様に401を返します。
Test Suite
packages/vercel-basic-auth/src/index.test.ts
beforeAuthコールバックのテストケース(6つ)とafterAuthコールバックのテストケース(3つ)を追加。各フックの戻り値に応じた動作(401応答またはパス)を検証します。

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant basicAuth as basicAuth<br/>Middleware
    participant beforeAuth as beforeAuth<br/>Hook
    participant validate as Credential<br/>Validation
    participant afterAuth as afterAuth<br/>Hook

    Client->>basicAuth: Request with/without Auth Header
    basicAuth->>beforeAuth: Call beforeAuth(request)
    
    alt beforeAuth returns false
        beforeAuth-->>basicAuth: false
        basicAuth-->>Client: 401 Unauthorized
    else beforeAuth returns true or undefined
        beforeAuth-->>basicAuth: true/undefined
        basicAuth->>validate: Validate credentials
        
        alt Credentials valid
            validate-->>basicAuth: ✓
            basicAuth->>afterAuth: Call afterAuth(request)
            
            alt afterAuth returns false
                afterAuth-->>basicAuth: false
                basicAuth-->>Client: 401 Unauthorized
            else afterAuth returns true or undefined
                afterAuth-->>basicAuth: true/undefined
                basicAuth-->>Client: null (Allow)
            end
        else Credentials invalid
            validate-->>basicAuth: ✗
            basicAuth-->>Client: 401 Unauthorized
        end
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Suggested reviewers

  • akameco

Poem

🐰 認可の前にも後にも、
自由な判定が走る今ぞ、
beforeAuth、afterAuth、
コールバック、ぴょん!ぴょん!
認証の門、もっと柔軟に〜🔐✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed PRタイトルは、beforeAuth/afterAuthコールバックの追加という主要な変更を正確に説明しており、変更内容と完全に関連している。
Linked Issues check ✅ Passed PR #23は#21の要件を満たしており、beforeAuth/afterAuthコールバックで認証フロー前後のカスタム判定機能を実装している。
Out of Scope Changes check ✅ Passed すべての変更は#21のコールバック引数受け入れという目的に関連しており、スコープ外の変更は見当たらない。

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feature/vercel-basic-auth-callbacks

Comment @coderabbitai help to get the list of available commands and usage tips.

@amotarao amotarao marked this pull request as draft March 29, 2026 06:47
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@packages/vercel-basic-auth/src/index.ts`:
- Around line 16-20: The JSDoc for beforeAuth is incorrect about when it's
invoked; update the comment to state that beforeAuth is executed before the
built-in environment check (i.e., called prior to any environment gating) and
that returning true continues the normal auth flow while false returns a 401.
Locate the beforeAuth parameter/option in this module (the beforeAuth callback
referenced in the exported auth setup) and change the JSDoc text to reflect
"called before environment check" and keep the existing behavior description
(true -> proceed, false -> return 401).
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 7df4e47d-dfc4-4902-ae8a-67a8b31217e6

📥 Commits

Reviewing files that changed from the base of the PR and between c7a6d22 and f090aca.

📒 Files selected for processing (3)
  • .changeset/vercel-basic-auth-callbacks.md
  • packages/vercel-basic-auth/src/index.test.ts
  • packages/vercel-basic-auth/src/index.ts

Comment on lines +16 to +20
/**
* 組み込みの環境判定後、認証チェック前に呼ばれるコールバック
* - true を返すと通常の認証フローに進む
* - false を返すと認証失敗 (401) を返す
*/
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

beforeAuth の JSDoc が実装順序と逆です。

Line 17 は「環境判定後」となっていますが、実装は Line 50 で環境判定より前に beforeAuth を実行しています。利用者向け仕様説明が誤るため修正してください。

修正案(JSDoc のみ)
   /**
-   * 組み込みの環境判定後、認証チェック前に呼ばれるコールバック
+   * 組み込みの環境判定前に呼ばれるコールバック
    * - true を返すと通常の認証フローに進む
    * - false を返すと認証失敗 (401) を返す
    */
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
/**
* 組み込みの環境判定後、認証チェック前に呼ばれるコールバック
* - true を返すと通常の認証フローに進む
* - false を返すと認証失敗 (401) を返す
*/
/**
* 組み込みの環境判定前に呼ばれるコールバック
* - true を返すと通常の認証フローに進む
* - false を返すと認証失敗 (401) を返す
*/
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/vercel-basic-auth/src/index.ts` around lines 16 - 20, The JSDoc for
beforeAuth is incorrect about when it's invoked; update the comment to state
that beforeAuth is executed before the built-in environment check (i.e., called
prior to any environment gating) and that returning true continues the normal
auth flow while false returns a 401. Locate the beforeAuth parameter/option in
this module (the beforeAuth callback referenced in the exported auth setup) and
change the JSDoc text to reflect "called before environment check" and keep the
existing behavior description (true -> proceed, false -> return 401).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

vercel-basic-auth: 独自の追加判定をしたいときの cb 引数受け入れ

1 participant