Cloudflare + React Router v7 and Better Auth

It seems there is no clear answer on how to actually use Better Auth with Cloudflare D1. As a beginner, I have searched high and low for what is seemingly an impossible answer.

  1. The problem starts with Better Auth: Better Auth does not support Cloudflare D1 out of the box
  2. Third-party packages are required: Both Kysely and Kysely-D1 are needed to do something with Better Auth and unfortunatly the documentation is non-existent as to how Kysley talks to Cloudflare D1 and Better Auth or why these third-party packages are required
  3. Lack of a clear way forward: I have found several resources and claim "Better Auth + Cloudflare D1, just add water" however when reviewing the project setups every Better Auth and Kysely conguration is different
What I'm trying to achieve is simply add Better Auth into a React Router v7 project with the default e-mail/password authentication so I can protect routes.

From the Better Auth documentation I am able to only get to step 3 (https://www.better-auth.com/docs/installation)
  • npm install better-auth
  • created .dev.vars in project root; add the variables (generate secret key, url is http://localhost:5173)
  • created auth.ts in app/utils
Step 4 is to configure the database. There is basically no clear way forward here.

  • Further reading leads me to the other supported databases pages, https://www.better-auth.com/docs/adapters/other-relational-databases
  • "Better Auth supports a wide range of database dialects out of the box thanks to Kysely." - but as a beginner, I have no idea what this actually means
  • Under *Kysely Community dialects* I see "Cloudflare D1", https://github.com/aidenwallis/kysely-d1
  • https://github.com/aidenwallis/kysely-d1 has only 1 instruction, "npm i kysely-d1" but no other context for where to put that code that's in the readme.md file. Replace auth.ts?
Does anybody have a clear way to connect Better Auth with Cloudflare D1 that does not involve other packages like Hono or Drizzle?
Learn how to configure Better Auth in your project.
Integrate Better Auth with other relational databases.
GitHub
D1 dialect for Kysely. Contribute to aidenwallis/kysely-d1 development by creating an account on GitHub.
Was this page helpful?