Skip to content

Add authentication section to Agents docs#28654

Closed
deathbyknowledge wants to merge 3 commits into
cloudflare:productionfrom
deathbyknowledge:agents-auth
Closed

Add authentication section to Agents docs#28654
deathbyknowledge wants to merge 3 commits into
cloudflare:productionfrom
deathbyknowledge:agents-auth

Conversation

@deathbyknowledge
Copy link
Copy Markdown
Contributor

@deathbyknowledge deathbyknowledge commented Feb 27, 2026

Summary

Adds a new Authentication section to the Agents documentation, replacing the single-page auth guide with a dedicated section containing focused pages for each approach:

  • Overview (authentication/index.mdx) — Decision table for choosing an auth approach, core principle (authenticate before the Agent), and best practices.
  • Token and middleware auth (authentication/token-and-middleware.mdx) — Static token validation, JWT verification, passing tokens from clients, per-instance authorization, and custom routing with Hono middleware.
  • Workers OAuth Provider (authentication/oauth-provider.mdx) — OAuth 2.1 authorization server setup, third-party IdP integration (GitHub example), token scoping and revocation.
  • Full-stack auth with Better Auth (authentication/better-auth.mdx) — Complete tutorial for user management with sign-up/sign-in, D1 database setup, JWT issuance, and client integration.

Screenshots (optional)

Documentation checklist

  • Is there a changelog entry (guidelines)? If you don't add one for something awesome and new (however small) — how will our customers find out? Changelogs are automatically posted to RSS feeds, the Discord, and X.
  • The change adheres to the documentation style guide.
  • If a larger change - such as adding a new page- an issue has been opened in relation to any incorrect or out of date information that this PR fixes.
  • Files which have changed name or location have been allocated redirects.

@github-actions github-actions Bot added product:agents Build and deploy AI-powered Agents on Cloudflare that can act autonomously. size/l labels Feb 27, 2026
@deathbyknowledge deathbyknowledge changed the title add auth docs to agents Add authentication section to Agents docs Mar 10, 2026
@deathbyknowledge deathbyknowledge marked this pull request as ready for review March 10, 2026 11:23
@deathbyknowledge deathbyknowledge requested a review from a team March 10, 2026 11:23
@deathbyknowledge deathbyknowledge requested a review from a team as a code owner March 10, 2026 11:23
- You need session management, password hashing, and JWT issuance in one package.
- You are building a product where users create accounts and interact with personalized Agent instances.

You can view the full example code on [GitHub](https://github.com/cloudflare/agents/tree/main/examples/auth-agent).
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sunil ended up rewriting the example. Either we change the docs to follow the example or just don't mention the example

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Good catch — confirmed the auth-agent example was rewritten in cloudflare/agents#948 and now ships as a lightweight JWT demo (no Better Auth). Addressed in the replacement PR #30053 by removing the two links to the example from better-auth.mdx. Also fixed the auth-agent description in index.mdx (it incorrectly said "Complete working example with Better Auth"), and added a cross-link from token-and-middleware.mdx since the rewritten example actually demonstrates that page's JWT + onBeforeConnect / onBeforeRequest pattern.


:::note[Lazy initialization]

The `getAuth()` function uses a lazy singleton pattern because `betterAuth()` accesses environment bindings (like `env.AUTH_DB`) that are only available inside a request context on Workers, not at module scope.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't believe this is fully true. We can use

import { env } from "cloudflare:workers"

There are other reasons for using s lazy singleton pattern though. I think it's best explained in the changes in this commit

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You're right — import { env } from "cloudflare:workers" does give module-scope access to bindings, so the original note was inaccurate. Rewrote it in the replacement PR #30053 using the reasoning from the commit you linked: the real reason for the lazy singleton is that betterAuth()'s internal init() does async I/O (telemetry filesystem detection via dynamic import("fs/promises")) that only resolves inside a request context — calling betterAuth() at module scope causes auth.handler() to hang indefinitely. Also updated the inline code comment above the snippet to match.

@github-actions
Copy link
Copy Markdown
Contributor

Hey there, we've marked this pull request as stale because there's no recent activity on it. This label is helps us identify PRs that might need updates (or to be closed out by our team if no longer relevant).

@github-actions github-actions Bot added the stale label Mar 25, 2026
Muhammad-Bin-Ali pushed a commit that referenced this pull request Apr 17, 2026
Adds a dedicated Authentication section with focused pages for each approach:

- Overview (authentication/index.mdx) with decision table and best practices.
- Token and middleware auth (authentication/token-and-middleware.mdx) covering
  static token validation, JWT verification, passing tokens from clients,
  per-instance authorization, and Hono middleware.
- Workers OAuth Provider (authentication/oauth-provider.mdx) covering OAuth 2.1
  authorization server setup, third-party IdP integration (GitHub example),
  and token scoping and revocation.
- Full-stack auth with Better Auth (authentication/better-auth.mdx) with a full
  tutorial for user management, D1 setup, JWT issuance, and client integration.

This replaces the stale PR #28654. Incorporates review feedback:

- Removed links to the cloudflare/agents auth-agent example from
  better-auth.mdx; the example was rewritten as a lightweight JWT demo and no
  longer demonstrates Better Auth.
- Corrected the 'Lazy initialization' note in better-auth.mdx. Environment
  bindings are available at module scope via 'import { env } from
  "cloudflare:workers"'. The real reason for the lazy singleton is that
  betterAuth()'s init() does async I/O (telemetry filesystem detection via
  dynamic import("fs/promises")) that only resolves inside a request context.
- Added a 'Complete example' link to token-and-middleware.mdx pointing at the
  auth-agent example, since that page describes the JWT pattern the example
  now demonstrates.
- Updated the auth-agent description in index.mdx to reflect that it
  demonstrates JWT auth, not Better Auth.
@haleycode
Copy link
Copy Markdown
Contributor

Closed in favor of #30053 so @Muhammad-Bin-Ali could incorporate outstanding comments.

@haleycode haleycode closed this Apr 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

product:agents Build and deploy AI-powered Agents on Cloudflare that can act autonomously. size/l stale

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants