[bug] Better Auth addon generates legacy D1 binding access with SvelteKit 3 and Cloudflare adapter 8
I maintainer di solito rispondono entro 4 giorni
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Idoneità per principianti
- 82/100
- Tipo di issue
- Bug
- Chiarezza
- Specificata chiaramente
- Stato di attività
- Attiva
- Stack tecnologico
- typescript
Direzione di ricerca
Trova il modello dell’addon Better Auth che genera src/hooks.server.ts per Cloudflare Workers e Drizzle D1. Inizia cercando event.platform.env.DB nei template della CLI e controlla come accedono ai binding gli altri template Cloudflare. Aggiorna l’hook generato affinché usi l’API dei binding supportata, quindi verifica che un progetto generato si avvii e che le sue route restituiscano HTTP 200.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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.
- Lingua principale
- TypeScript
- Stelle
- 513
- Fork
- 110
- Merge medio
- 5g 1h
- PR unite (30g)
- 24
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di sveltejs/cli
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
I maintainer di solito rispondono entro 4 giorni
-
pkg:add
Difficoltà 2/5 1-3 ore Idoneità per principianti 85/100
sveltejs/cli#1404 · 1 commento ·
I maintainer di solito rispondono entro 4 giorni
-
pkg:add
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 78/100
sveltejs/cli#1399 · 2 commenti ·
I maintainer di solito rispondono entro 4 giorni
-
pkg:migrate
Difficoltà 3/5 1-2 giorni Idoneità per principianti 35/100
sveltejs/cli#1461 · 2 commenti ·
I maintainer di solito rispondono entro 4 giorni
-
enhancement pkg:migrate
Difficoltà 3/5 1-2 giorni Idoneità per principianti 53/100
sveltejs/cli#1440 · 3 commenti · 1 reazione ·
I maintainer di solito rispondono entro 4 giorni
Tutte le issue di sveltejs/cli
Issue simili
-
[analytics-core] getQueryParams does not decode `+` as space and truncates values containing `=`Apertabug
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
amplitude/Amplitude-TypeScript#2179 · 1 commento ·
I maintainer di solito rispondono entro 2 giorni
-
ci good first issue severity: low
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
TMHSDigital/GH-Arcade#111 ·
-
AsnIntegerConverter.toASN coerces strings with +value, rounding integers outside safe-integer rangeAperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
PeculiarVentures/asn1-schema#189 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
anaisbetts/spawn-rx#38 ·