[bug] Better Auth addon generates legacy D1 binding access with SvelteKit 3 and Cloudflare adapter 8

オープン 初心者向け
#1,450 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 4 日以内に返信

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
82/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
typescript
領域
backend, cli

調査の方向性

Cloudflare Workers と Drizzle D1 向けに src/hooks.server.ts を生成する Better Auth アドオンのテンプレートを見つけてください。まず CLI テンプレートで event.platform.env.DB を検索し、ほかの Cloudflare テンプレートがどのようにバインディングにアクセスしているか確認してください。生成されるフックを、サポートされているバインディング API を使うように更新し、生成したプロジェクトが起動して、そのルートが HTTP 200 を返すことを確認してください。

索引モデルが issue の本文から書いたものです。

説明

bug pkg:add

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

  1. Run:

    npx [email protected] create
    
  2. Select the following options:

    • Template: SvelteKit minimal
    • TypeScript: Yes
    • Adapter: Cloudflare → Workers
    • Drizzle: Cloudflare D1
    • Better Auth: Email & Password
  3. Configure the Cloudflare D1 binding and environment variables as instructed by the CLI.

  4. Start the development server:

    npm run dev
    
  5. 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.

主要言語
TypeScript
スター
513
フォーク
110
平均マージ
5日 1時間
マージ済み PR(30日)
24

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

sveltejs/cli のほかの issue

sveltejs/cli の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。