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

Open Beginner friendly
#1,450 2 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 4 days

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
82/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
typescript
Domain
backend, cli

Research direction

Find the Better Auth addon template that generates src/hooks.server.ts for Cloudflare Workers and Drizzle D1. Start by searching the CLI templates for event.platform.env.DB and check how other Cloudflare templates access bindings. Update the generated hook to use the supported binding API, then verify a generated project starts and its routes return HTTP 200.

Written by the indexing model from the issue text.

Description

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.

Dominant language
TypeScript
Stars
513
Forks
110
Avg merge
5d 1h
Merged PRs (30d)
24

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from sveltejs/cli

All issues in sveltejs/cli

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.