Is there a way to get bindings outside the `c` Context of an inbound Request in Cloudflare Workers?
I'm trying to setup the better-auth with D1 but following the guide it pops out that I need to have a valid reference of my binded D1 database outside of the inbount request context (the c.env).
There are other examples of using D1 with better-auth but I can't figure out how to make it work within a Hono app deployed on Cloudflare Workers.
The configuration file is within
/src/lib/auth.ts
but isn't called directly nowhere within the Hono app
I have a worker-configuration.d.ts
in my src
directory declaring the DB as a D1Database type, and the wrangler binding as well.8 Replies
Why don't you store the auth instance in the context by
.set()
using a middleware? That why you will also get the context
object as well as a single instance will be used across your requestNo luck, no matter what I try seems impossible to get a reference of the BIndings, even with the docs about contextStorage() it crashes the app on runtime with
service core:user:app: Uncaught Error: Context is not available
But you will setting the value inside a middleware, so you will have access to the bindings. Rough Example - . Dont forget to add
auth
in hono variables
Like this Hono<{ Variables: { auth: any } }>()
Mmm, not sure this is an optimal way, only imports of
betterAuth
and Kysely
are about half MB of bundle size, again not sure if this is a heavy lift to the whole app, since being it a middleware will execute the setup each time a request comes in, also this lib has handy methods to manage state for Hono, but no reference of D1 Binding within HonoYeah, this is perfect - https://www.better-auth.com/docs/integrations/hono#mount-the-handler, just init the auth inside the function. honestly regarding the function if you are not running this on node then you are still init the auth on each request. If you are on node then you can just store the auth instance as a global variables once init.
Hono Integration | Better Auth
Hono Integration Guide
Unfortunatelly it won't work, the lib requires you to export an
auth
object from a auth.ts
file to be able to run the CLI tool for migration against it, if I define the auth
object within a middleware I can't export it and the CLI isn't able to find it.
It also requires me to store another Variable in the context with c.set("auth", auth)
besides the session
and user
already stored in the middleware example to be able to use the handler for the /api/auth/**
routes becoming a strange antipattern.Then I think you should either ask them in their discord or create a discussion/issue on their GitHub regarding this.
Got some updates, I did open a GH issue, for everyone landing here you can keep track of the progress here
GitHub
Can't bind D1 Database on Hono with Cloudflare Workers · Issue #207...
After some attempts, I couldn't find a way to bind a D1 Database to better-auth on a Hono app deployed to Cloudflare Workers. Following the official instructions, the installation step is commo...