Skip to content

docs: add auth and context - #112

Merged
pi0 merged 8 commits into
h3js:mainfrom
sandros94:docs-auth-example
Jun 17, 2026
Merged

pi0 merged 8 commits into
h3js:mainfrom
sandros94:docs-auth-example

Conversation

@sandros94

@sandros94 sandros94 commented Jan 23, 2025 •

Copy link
Copy Markdown
Member

Resolves #88

Should I also add an auth example via cookie?

I might need some help on the ok: false backward compatible thing

Summary by CodeRabbit

  • Documentation
    • Updated the Hooks guide with a revised close(peer, details) hook example (replacing the prior naming)
    • Documented returning a connection-scoped context from upgrade, available as peer.context in later hooks
    • Added guidance for performing authentication during upgrade, including blocking unauthorized upgrades with a 401 response and WWW-Authenticate (Basic auth example)

@codecov

codecov Bot commented Jan 23, 2025 •

Copy link
Copy Markdown

Codecov Report

All modified and coverable lines are covered by tests ✅

Please upload report for BASE (main@9a8d499). Learn more about missing BASE report.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #112   +/-   ##
=======================================
  Coverage        ?   76.23%           
=======================================
  Files           ?        9           
  Lines           ?      711           
  Branches        ?      145           
=======================================
  Hits            ?      542           
  Misses          ?      167           
  Partials        ?        2           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Comment thread docs/1.guide/2.hooks.md Outdated
@travis-r6s

Copy link
Copy Markdown

Thanks @sandros94, this gave me some much needed guidance! However, I found that setting the context to an object doesn't work, you have to set specific properties. For example (using crossws@0.3.3):

Trying to set request.context to a new object:

upgrade(request) {
  request.context = { data: 'test'}
},
open(peer) {
  console.log(peer.context) // logs {}
}

Setting a specific property in request.context:

upgrade(request) {
  request.context.data = 'test'
},
open(peer) {
  console.log(peer.context) // logs { data: 'test' }
}

Co-authored-by: Travis Reynolds <travis@travisreynolds.dev>
@sandros94

Copy link
Copy Markdown
Member Author

@travis-r6s thanks for reporting it!

Indeed context is already initialized 😅

@pi0 pi0 mentioned this pull request Jan 29, 2025
@pi0

pi0 commented Jan 29, 2025

Copy link
Copy Markdown
Member

Good point @travis-r6s ~> #115

Comment thread docs/1.guide/2.hooks.md Outdated
@coderabbitai

coderabbitai Bot commented Jun 17, 2026 •

Copy link
Copy Markdown

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: b27bda55-111e-4470-ad91-92ebf8e6a746

📥 Commits

Reviewing files that changed from the base of the PR and between 15ca397 and d5cd447.

📒 Files selected for processing (1)
  • docs/1.guide/2.hooks.md

📝 Walkthrough

Walkthrough

The "Hooks" guide is updated to rename the close hook parameter from event to details in the example, and two new sections are added: one documenting how to return a context object from upgrade for use as peer.context in subsequent hooks, and one demonstrating authentication by returning a 401 Response during upgrade.

Changes

Hooks Guide Documentation Update

Layer / File(s) Summary
Close hook parameter rename
docs/1.guide/2.hooks.md
The close hook example parameter is renamed from event to details, and the error hook example is retained.
Upgrade context and authentication documentation
docs/1.guide/2.hooks.md
Adds a section on returning a context object from upgrade (accessible as peer.context in open, message, and close hooks, with a volatility note for some environments) and an Authentication section showing how to return a Response(401) with a WWW-Authenticate header to block upgrades when Basic-auth credentials are missing or invalid.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐇 A hop and a skip through the hooks so grand,
Where close and details now walk hand in hand!
With context blooming and auth standing tall,
The WebSocket guide now covers it all.
Credentials checked, connections blessed—
CrossWS docs are truly the best! ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'docs: add auth and context' directly references the two main documentation additions (authentication and context handling) covered in the PR changes.
Linked Issues check ✅ Passed The PR documentation comprehensively addresses issue #88's requirements by providing clear guidance on authentication handling in WebSocket connections and context object usage.
Out of Scope Changes check ✅ Passed All changes are within scope—the PR updates only the hooks documentation to add authentication and context handling examples directly requested in issue #88.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@pi0
pi0 merged commit 2643005 into h3js:main Jun 17, 2026
3 of 5 checks passed
@pi0x pi0x mentioned this pull request Aug 21, 2026
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.

Describe how authentication should be handled

4 participants