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.
- Clone the repository. https://github.com/xkema/demo-better-auth-cloudflare-d1-kysely-quickfixes-sveltekit
- Run
npm install. - Add an
.envfile and addBETTER_AUTH_SECRETandBETTER_AUTH_URLenvironmental variables. (SvelteKit's default preview address ishttp://localhost:5173.) You may use the.env.exampleas a template. - Add a
wrangler.jsoncfile and add adatabase_nameanddatabase_id. (No need for an actual DB at development environment, just fill up with some name and identifier, e.g.name-of-your-databaseandid-of-your-database.) You may use thewrangler.jsonc.exampleas a template. - You're now at the "Create Database Tables" step of the Better Auth's official installation guide.
- If you try to run the
npx @better-auth/cli generateCLI 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. - Run the custom
generatescriptnpm run better-auth:generate:hacked. - Check the
./better-auth_migrationsdirectory. You should see the generated script if everything went well. (This directory is.gitignored.) - Create a D1 migration with the
npx wrangler d1 migrations create name-of-your-database better_auth_initializationcommand. - Copy the content of
./better-auth_migrations/*.sqlfile into the./migrations/0001_better_auth_initialization.sqlfile. - You may also copy the generated script, instead of creating a new migration. I use this flow.
- List the migrations with the
npx wrangler d1 migrations list name-of-your-databasecommand. - Apply the migrations with
npx wrangler d1 migrations apply name-of-your-databasecommand. - You may now open your database manager application and see the changes. I use https://dbeaver.io/.
- Now Better Auth database is setup. Start development server with
npm run devto see sign-in/sign-out actions are working correctly. (SvelteKit's default preview address ishttp://localhost:5173.) - If everything is intact, check the code how it setup and quickfixed.
- Ping me on GitHub if you find anything broken.
- 👋
At this stage, you should be able to:
- Preview the demo with the framework's development server using
npm run dev. - Build the project using
npm run build. - Preview the demo with
wrangler's development server by runningnpm run preview. - Add a new Better Auth plugin to the common config and generate an updated migration script with the custom
npm run better-auth:generate:hackedmigration script, for example, add theorganization()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:
- Accessing the D1 database instance outside the Cloudflare's
workerdruntime. - Not able to use the
npx @better-auth/cli@latest generateCLI 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:
- D1-related table access error
SQLITE_AUTHraised by Kysely database introspection method. - Unable to run the actual database queries in the context of the
npx @better-auth/cli@latest generatecommand, which is aNode.jsruntime, notworkerd. - Unable to import related Cloudflare modules (
cloudflare:workersandwrangler) into the Better Auth configuration module.
The quickfixes added to overcome these three are a bit more complicated.
- For the
SQLITE_AUTHaccess error, I intercepted database requests and added an exclusion for the_cf_METADATAtable which causes the error. - For the CLI
generatecommand errors, I had to rewrite a copycatgeneratecommand by using thegetPlatformProxy()helper provided by Cloudflare to overcome such problems. (Only on development/test environments.) - Lastly, for the module import problem, I have no quickfixes, but the first two quickfixes kind of avoided it. The error about the
cloudflare:workersimport is understandable; however, the error we're getting when importing from thewranglermodule must be a Better Auth-related problem.
And that's the wrap. Thank you if you've read everything. 😄
- Setup the project.
- Do another demo with
Next.js.