A demo page with the same code on Cloudflare Workers environment demonstrates basic sign-in/sign-out functionality with the Anonymous plugin using SvelteKit's remote functions feature.
https://demo-better-auth-cloudflare-d1-kysely-quickfixes-sveltekit.missofis.workers.dev/
The fixes presented in this repository should be considered quickfixes/patches, not as solutions. They're not maintainable, require a lot of side work and make package updates painful. I discourage you to rely on this level of patching on production environments.
npm install..env file and add BETTER_AUTH_SECRET and BETTER_AUTH_URL environmental variables. (SvelteKit's default preview address is http://localhost:5173.) You may use the .env.example as a template.wrangler.jsonc file and add a database_name and database_id. (No need for an actual DB at development environment, just fill up with some name and identifier, e.g. name-of-your-database and id-of-your-database.) You may use the wrangler.jsonc.example as a template.npx @better-auth/cli generate CLI command, you'll get an error since we're using a custom Better Auth configuration instance. Instead, use the custom Node.js script ./bin/generate.js.generate script npm run better-auth:generate:hacked../better-auth_migrations directory. You should see the generated script if everything went well. (This directory is .gitignored.)npx wrangler d1 migrations create name-of-your-database better_auth_initialization command../better-auth_migrations/*.sql file into the ./migrations/0001_better_auth_initialization.sql file.npx wrangler d1 migrations list name-of-your-database command.npx wrangler d1 migrations apply name-of-your-database command.npm run dev to see sign-in/sign-out actions are working correctly. (SvelteKit's default preview address is http://localhost:5173.)At this stage, you should be able to:
npm run dev.npm run build.wrangler's development server by running npm run preview.npm run better-auth:generate:hacked migration script, for example, add the organization() plugin and run the script after you've applied the previous migration to the local database. The newly generated script should include the new tables (create) and table updates (alter) for existing ones.The Better Auth version used in this repository is 1.3.34.
There are two main challenges when trying to set up Better Auth in a Cloudflare environment with D1 and Kysely:
workerd runtime.npx @better-auth/cli@latest generate CLI command.The first one (1) is an expected and acceptable problem because Cloudflare Workers do not allow I/O from outside a request context. Cloudflare Docs | How to access env
The quickfix added here is moving the betterAuth({...}) instantiation step into the request context, which causes the instance to be created again and again on every Better Auth request passing through the worker. That's all I could find as a quickfix.
The second one (2) is more challenging, because you need to overcome three additional problems:
SQLITE_AUTH raised by Kysely database introspection method.npx @better-auth/cli@latest generate command, which is a Node.js runtime, not workerd.cloudflare:workers and wrangler) into the Better Auth configuration module.The quickfixes added to overcome these three are a bit more complicated.
SQLITE_AUTH access error, I intercepted database requests and added an exclusion for the _cf_METADATA table which causes the error.generate command errors, I had to rewrite a copycat generate command by using the getPlatformProxy() helper provided by Cloudflare to overcome such problems. (Only on development/test environments.)cloudflare:workers import is understandable; however, the error we're getting when importing from the wrangler module must be a Better Auth-related problem.And that's the wrap. Thank you if you've read everything. 😄
Next.js.