feat: add Cloudflare Email support for Better Auth#57
Conversation
commit: |
zpg6
left a comment
There was a problem hiding this comment.
Nice work, this is clean. And thanks for adding to CLI capabilities too. A few things I'd want changed before merge:
-
Configuring
emailsetsemailAndPassword.enabled: trueeven for users who never setemailAndPasswordat all. Someone on OAuth/magic-link/OTP would get password auth turned on without asking. -
opennextjs example passes
emailunconditionally, while Hono only sets it when the binding exists (env?.EMAIL). Could we guard it the same way Hono does so the two examples behave consistently? -
Default templates could use a "if you didn't request this, ignore this email". better-auth's demo has this.
-
The "domain must be onboarded in Cloudflare Email Service" note is in the CLI readme/docs but there's no actual link to CF's docs, and nothing in the main README. Maybe add something..
There was a problem hiding this comment.
Pull request overview
This PR adds first-class Cloudflare Email Sending support to better-auth-cloudflare, including typed helpers to send transactional emails and CLI + example updates to generate/configure the required Wrangler bindings.
Changes:
- Added
CloudflareEmailConfigtypes pluscreateEmailSender/createEmailOptions, and wiredwithCloudflareto auto-fill Better Auth email callbacks when configured. - Extended the CLI to generate/update Wrangler + env typings + auth scaffolding for Email Sending (flags, templates, validation), with added Bun test coverage.
- Updated documentation and examples to include Email Sending bindings, env vars, and Better Auth configuration.
Reviewed changes
Copilot reviewed 21 out of 23 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| src/types.ts | Adds Cloudflare Email config/types to public API surface. |
| src/index.ts | Wires email options into withCloudflare and re-exports email helpers. |
| src/email.ts | Implements email sender + Better Auth option wiring helpers. |
| README.md | Documents Email support and updates example configuration snippets. |
| package.json | Updates package description/keywords to include Email. |
| examples/opennextjs/wrangler.toml | Adds [[send_email]] binding and BETTER_AUTH_EMAIL_FROM. |
| examples/opennextjs/src/auth/index.ts | Adds Email config + Better Auth email options in example. |
| examples/opennextjs/env.d.ts | Adds SendEmail binding + BETTER_AUTH_EMAIL_FROM typing. |
| examples/hono/wrangler.toml | Adds [[send_email]] binding and BETTER_AUTH_EMAIL_FROM. |
| examples/hono/src/env.d.ts | Adds SendEmail binding + BETTER_AUTH_EMAIL_FROM typing. |
| examples/hono/src/auth/index.ts | Adds optional Email wiring + email verification config. |
| docs/configuration.md | Documents Email option and exported helpers; updates override behavior table. |
| cli/tests/wrangler-generator.test.ts | Adds tests for Email Sending TOML generation. |
| cli/tests/non-interactive-args.test.ts | Adds tests for new --email* flags and binding validation. |
| cli/tests/env-d-generator.test.ts | Adds tests for env typing generation with Email enabled. |
| cli/tests/email-helper.test.ts | Adds Bun tests for email helpers and default templates. |
| cli/tests/auth-generator.test.ts | Adds tests ensuring generated auth code includes Email wiring. |
| cli/src/lib/wrangler-generator.ts | Generates [[send_email]] + BETTER_AUTH_EMAIL_FROM in TOML output. |
| cli/src/lib/typescript-validator.ts | Prefers local TypeScript compiler when validating generated projects. |
| cli/src/lib/env-d-generator.ts | Generates env typings for Email binding + sender address var. |
| cli/src/lib/auth-generator.ts | Adds Email wiring to generated auth configuration for templates. |
| cli/src/index.ts | Adds interactive + non-interactive CLI support for Email flags/inputs. |
| cli/README.md | Documents Email flags, behavior, and usage examples. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| const sender = createEmailSender(email); | ||
| const emailVerification: EmailVerificationOptions = { ...(options.emailVerification ?? {}) }; | ||
| const emailAndPassword: EmailAndPasswordOptions = { enabled: true, ...(options.emailAndPassword ?? {}) }; | ||
|
|
||
| if (email.sendVerificationEmail !== false && !emailVerification.sendVerificationEmail) { | ||
| emailVerification.sendVerificationEmail = async ({ user, url, token }, request) => { | ||
| const content = await (email.templates?.verification ?? defaultVerificationTemplate)({ | ||
| user, | ||
| url, | ||
| token, | ||
| request, | ||
| }); | ||
| await sender({ | ||
| to: user.email, | ||
| ...content, | ||
| }); | ||
| }; | ||
| } | ||
|
|
||
| if (email.sendResetPassword !== false && !emailAndPassword.sendResetPassword) { |
| | `advanced` | Merges your `authOptions.advanced` with IP detection headers when `autoDetectIpAddress` is enabled. | | ||
| | `session` | Merges your `authOptions.session`, forcing `storeSessionInDatabase: true` when `geolocationTracking` is enabled — even if you explicitly set it to `false`. | | ||
| | `emailVerification` | Adds a Cloudflare Email `sendVerificationEmail` callback when `email` is configured and no custom callback exists. | | ||
| | `emailAndPassword` | Adds a Cloudflare Email `sendResetPassword` callback when `email` is configured and no custom callback exists. | |
Hi ! |
Yes, ofc ! Anything else ? I just saw copilot introduced some comments, just reviewing them and then i work on it ! |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
I don't think copilot recommended anything I didn't already cover. make sure you update all relevant docs. you can resolve copilots review and mine once you fix. no worries about opennextjs artifacts if you implement the same way as you do hono (but still want to see something) |
|
Okay, just implemented everything per your request, let me know if you want anything else changed |
opennextjs template generates
Don't see the "if you didn't request this, ignore this email" line if you agreed with me that was a fair default. |
|
Hi @zpg6 , can you review the pull request for merge ? |
|
@zpg6 any updates ? |
Summary
This PR implements the Cloudflare Email roadmap item by adding a typed Email Sending integration for Better Auth transactional emails.
What changed
Added Cloudflare Email sender helpers and types:
createEmailSendercreateEmailOptionsCloudflareEmailConfigWired
withCloudflareto fill Better Auth email callbacks whenemailis configured:Added CLI generation support for Email:
--email--email-binding--email-fromGenerated the correct Wrangler Email Sending TOML shape:
Updated Hono and OpenNext examples with Email bindings, env typings, and auth config.
Updated README, CLI README, configuration reference, and package metadata.
Added Bun test coverage for Email helpers and CLI generation.
Fixed CLI TypeScript validation to prefer the local TypeScript compiler.
Validation
npm run typechecknpm run typecheckincli89 pass, 0 failNotes
BETTER_AUTH_EMAIL_FROMmust belong to a domain configured in Cloudflare Email Service.