How to set up the database with drizzleAdapter + D1 using Hono on Cloudflare Workers
Hey folks, what's up?
I'm creating an API with authentication that runs on Cloudflare Workers. Initially, I configured Hono, Drizzle, and D1. But now I'm facing some errors when implementing authentication with better-auth.
Briefly, I'd like to know if anyone has successfully implemented something similar with these technologies.
Initially, I couldn't set up the database because the Cloudflare Workers environment only provides access to D1 through the context of a request (as far as I could understand from the Cloudflare documentation). I tried to work around the problem by creating a function to encapsulate the creation of the
auth
object and pass the D1 through the context.
However, it only "works" when I use this function directly, for example: getAuth().api.someFunction()
. When I try to authenticate through created routes, I get the error "Cannot read properties of undefined (reading 'DB')".
It seems that just encapsulating the creation of the auth object was a terrible idea because somehow better-auth (I didn't get to see the internal code of better-auth) expects and searches for the reference to this object by the name auth
from the auth.ts
file.12 Replies
Unknown User•3mo ago
Message Not Public
Sign In & Join Server To View
bascailly what you're doing is fine to use at runtime but to use the cli this won't be possible. One thing you could do is mount another endpoint where you call to migrate the schema. And call the migration manually by doing
Interesting, I did some tests and ended up populating the database manually. I'll test it this way.
Except for populating the database, I think I managed to get the authentication flow working.
At least I managed to create a new user and retrieve their session using Google.
Revisiting the code more carefully, I realized the database is being initialized incorrectly.
It was missing the step of creating the client with Drizzle first.
database: drizzleAdapter(c.env.DB, {
-> database: drizzleAdapter(drizzle(c.env.DB), {
I also adjusted the middleware for the session
Take a look to see if you're initializing Drizzle incorrectly, just like I did 😅
If I initialize it incorrectly, like the first time I did, I only receive a 500 error.
I forgot to mention, but I also changed this part
Unknown User•3mo ago
Message Not Public
Sign In & Join Server To View
Look, I'm not sure if there's a more elegant way to do this, but I just created a few types
Unknown User•2mo ago
Message Not Public
Sign In & Join Server To View
yeah baseURL should be inferred from a request, if it's not behind a proxy but curious what was the value you provided?
Unknown User•2mo ago
Message Not Public
Sign In & Join Server To View
Do you create this middleware in a separate file, and then import it to your Hono app? How do you use it within Hono?
nvm i got it
Ok I’m still having trouble, I think the auth instance isn’t persistent across requests. I can sign-in just fine, and accessing the session works, but on the next request using the middleware, I get a null session. How are you protecting your other routes using the middleware?
Well, I haven't "got to that part yet"
I did some initial tests to see what the authentication would be like and left it to finish after I finished the main part of the application
I'm using Astro to develop, my middleware is like this for now
I still need to see how this part still works in astro
doing an if for each route I need to reach doesn't seem like an elegant way to solve it, but it's what worked in my test