H
Hono6d ago
mihaaai

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.
import { betterAuth } from "better-auth";
import { anonymous } from "better-auth/plugins";
import { Kysely } from "kysely";
import { D1Dialect } from "kysely-d1";

export const auth = betterAuth({
database: {
db: new Kysely({
dialect: new D1Dialect({
database: process.env.DB as unknown as D1Database, // this results in database: undefined
}),
}),
type: "sqlite",
},
emailAndPassword: {
enabled: true,
},
plugins: [anonymous()],
});
import { betterAuth } from "better-auth";
import { anonymous } from "better-auth/plugins";
import { Kysely } from "kysely";
import { D1Dialect } from "kysely-d1";

export const auth = betterAuth({
database: {
db: new Kysely({
dialect: new D1Dialect({
database: process.env.DB as unknown as D1Database, // this results in database: undefined
}),
}),
type: "sqlite",
},
emailAndPassword: {
enabled: true,
},
plugins: [anonymous()],
});
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
Aditya Mathur
Aditya Mathur6d ago
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 request
mihaaai
mihaaai5d ago
No 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
Aditya Mathur
Aditya Mathur5d ago
But you will setting the value inside a middleware, so you will have access to the bindings. Rough Example -
app.use(async (c, next) => {
const auth = betterAuth({
// ...
database: c.env.DB
})

c.set("auth", auth)
})
app.use(async (c, next) => {
const auth = betterAuth({
// ...
database: c.env.DB
})

c.set("auth", auth)
})
. Dont forget to add auth in hono variables Like this Hono<{ Variables: { auth: any } }>()
mihaaai
mihaaai5d ago
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 Hono
Aditya Mathur
Aditya Mathur5d ago
Yeah, 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
mihaaai
mihaaai5d ago
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.
Aditya Mathur
Aditya Mathur5d ago
Then I think you should either ask them in their discord or create a discussion/issue on their GitHub regarding this.
mihaaai
mihaaai4d ago
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...
Want results from more Discord servers?
Add your server