auth-agent: lightweight JWT authentication example#948
Merged
Conversation
|
commit: |
ee9d5aa to
6a9e81e
Compare
deathbyknowledge
suggested changes
Feb 23, 2026
4be1e5b to
dc9304d
Compare
deathbyknowledge
approved these changes
Feb 25, 2026
Co-authored-by: deathbyknowledge <sjames@cloudflare.com>
Replace the previous better-auth + D1 example with a minimal JWT demo. Server now exposes a demo /api/token (HMAC HS256 via AUTH_SECRET) and verifies tokens in onBeforeConnect/onBeforeRequest; Durable Object renamed to ChatAgent and uses the token sub claim as the DO name. Client-side auth simplified to name-based token issuance/storage and UI adjusted accordingly. Removed D1 schema/sql and better-auth integration, updated env types, wrangler config (AI binding), package metadata, added favicon, and bumped some deps and UI styles. This refactors the example to be a smaller, copyable pattern for protecting WebSocket/HTTP agent routes while keeping the token issuance endpoint as demo scaffolding to be replaced in production.
1b41e0b to
6900f49
Compare
better-auth.
Contributor
|
this felt too heavy for an example, so I rewrote it to be jwt only and more as a guide. thanks for the pr! |
threepointone
approved these changes
Mar 2, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
examples/auth-agent— protect agent connections with JWTsExample showing how to authenticate WebSocket and HTTP connections to a Cloudflare Agent using JWTs.
What it demonstrates
onBeforeConnect— verify a JWT query param before upgrading to WebSocketonBeforeRequest— verify a JWT (Bearer header or query param) before handling HTTP requests/api/tokenendpoint that issues JWTs with jose (HMAC-SHA256,iss/audclaims)Key patterns
The token endpoint is demo scaffolding — in production, replace it with your own auth service. The
onBeforeConnect/onBeforeRequestmiddleware stays the same.Files
src/server.tssrc/auth-client.tssrc/client.tsxREADME.md