[bug] Better Auth addon generates legacy D1 binding access with SvelteKit 3 and Cloudflare adapter 8
Maintainer thường phản hồi trong vòng 4 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 82/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- typescript
Hướng nghiên cứu
Tìm mẫu addon Better Auth tạo src/hooks.server.ts cho Cloudflare Workers và Drizzle D1. Bắt đầu bằng cách tìm event.platform.env.DB trong các mẫu CLI và kiểm tra cách các mẫu Cloudflare khác truy cập bindings. Cập nhật hook được tạo để dùng API bindings được hỗ trợ, sau đó xác minh rằng dự án được tạo khởi chạy và các route của dự án trả về HTTP 200.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Describe the bug
When creating a new SvelteKit project using sv create with the Cloudflare Workers adapter, Drizzle (Cloudflare D1), and Better Auth (Email & Password) addons, the generated src/hooks.server.ts uses the legacy event.platform.env.DB API to access the D1 database binding.
With SvelteKit 3 and @sveltejs/adapter-cloudflare 8, Cloudflare-specific bindings are no longer exposed through event.platform.
As a result, the generated application fails with HTTP 500 errors during local development, even when the D1 binding is correctly configured.
Replacing event.platform.env.DB with env.DB imported from cloudflare:workers resolves the issue.
Steps to reproduce
-
Run:
npx [email protected] create -
Select the following options:
- Template:
SvelteKit minimal - TypeScript:
Yes - Adapter:
Cloudflare→Workers - Drizzle:
Cloudflare D1 - Better Auth:
Email & Password
- Template:
-
Configure the Cloudflare D1 binding and environment variables as instructed by the CLI.
-
Start the development server:
npm run dev -
Navigate to
http://localhost:5173/.
Actual behavior
The generated src/hooks.server.ts contains:
const handleBetterAuth: Handle = async ({ event, resolve }) => {
if (!event.platform?.env?.DB)
throw new Error('D1 binding "DB" not found - are you running with wrangler?');
event.locals.auth = createAuth(event.platform.env.DB);
// ...
};
The application throws the following runtime error:
Error: D1 binding "DB" not found - are you running with wrangler?
at handleBetterAuth (src/hooks.server.ts:23:9)
500 GET /
500 GET /favicon.ico
Expected behavior
The generated Better Auth integration should use the supported Cloudflare binding access API for SvelteKit 3 and @sveltejs/adapter-cloudflare 8.
A correctly configured project should start successfully without requiring manual modifications to the generated authentication hook.
Suggested fix
Import env directly from cloudflare:workers instead of accessing bindings through event.platform.env.
+ import { env } from 'cloudflare:workers';
const handleBetterAuth: Handle = async ({ event, resolve }) => {
- if (!event.platform?.env?.DB)
+ if (!env?.DB)
throw new Error('D1 binding "DB" not found - are you running with wrangler?');
- event.locals.auth = createAuth(event.platform.env.DB);
+ event.locals.auth = createAuth(env.DB);
// Remaining handler unchanged
};
After applying this change, the application successfully started, and the previously failing routes returned HTTP 200 instead of HTTP 500.
Environment
| Package | Version |
|---|---|
sv |
1.2.0 |
@sveltejs/kit |
3.1.1 |
@sveltejs/adapter-cloudflare |
8.0.2 |
better-auth |
1.7.16 |
| Database | Cloudflare D1 |
| ORM | Drizzle |
| Package manager | npm |
| OS | Windows (PowerShell) |
Additional context
The SvelteKit 3 migration guide documents the removal of Cloudflare-specific APIs from event.platform and recommends using imports from cloudflare:workers instead.
This appears to be a compatibility issue in the Svelte CLI Better Auth addon template when generating projects targeting SvelteKit 3 and Cloudflare Workers.
The addon template should be updated to generate the appropriate Cloudflare binding access code.
Note: The initial setup also reported a separate Drizzle schema mismatch involving missing Better Auth tables (user, session, account, and verification). This report specifically concerns the D1 binding access issue, which was resolved by the change described above.
- Ngôn ngữ chính
- TypeScript
- Star
- 513
- Fork
- 110
- Merge trung bình
- 5 ngày 1 giờ
- Pull request đã merge (30 ngày)
- 24
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của sveltejs/cli
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
Maintainer thường phản hồi trong vòng 4 ngày
-
pkg:add
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 85/100
sveltejs/cli#1404 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 4 ngày
-
pkg:add
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 78/100
sveltejs/cli#1399 · 2 bình luận ·
Maintainer thường phản hồi trong vòng 4 ngày
-
pkg:migrate
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 35/100
sveltejs/cli#1461 · 2 bình luận ·
Maintainer thường phản hồi trong vòng 4 ngày
-
clear `.svelte-kit` on migrationĐang mởenhancement pkg:migrate
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 53/100
sveltejs/cli#1440 · 3 bình luận · 1 reaction ·
Maintainer thường phản hồi trong vòng 4 ngày
Issue tương tự
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 66/100
devswha/herdr-web-ui#3875 ·
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 88/100
-
area: runtime chore cli consistency dependencies developer-experience difficulty: easy discovery documentation enhancement good first issue help wanted hygiene json-output packaging priority: P3 tests version
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 1 ngày
-
discovery:other factory-active task-identify-harness-labels-done task-identify-issue-type-done
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
vercel/ai#25068 · 4 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
create-strapi-app doesn't await writing license.txt, so a failed write skips its error handlingĐang mởflag: invalid template issue: bug severity: low source: cli status: pending reproduction version: 5
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
strapi/strapi#28637 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 2 ngày