How to use Lucia with Nuxt Hub ? (error: missing cloudflare db binding)
As the title suggest, I have the following error :
I tried to implement as per the documentation of Lucia, with some changes.
docs:
- https://hub.nuxt.com/docs/recipes/drizzle#usedrizzle
- https://lucia-auth.com/database/drizzle
- https://lucia-auth.com/getting-started/nuxt
Definition of hubDatabase() with similar error on line 38 : https://github.com/nuxt-hub/core/blob/main/src/runtime/server/utils/database.ts
reproduction link : https://stackblitz.com/~/github.com/rktmatt/help-drizzle-nuxt-hub
38 Replies
I was in the wrong doc. Apparently with D1 you have to do things a little bit differently for the adapter :
However, now instead of having an exported lucia util, I have a function to initialize and it doesn't work when initializing it with
export const lucia = initializeLucia(hubDatabase())
Hey burger! Im having the exact same issue
Stuck on it for 1-2 days now
I'm not even getting my bindings to properly work each and every time i open up the project (which might be a skill issue on my end)
It's kinda weird it's complaining about the D1 adapter, while in local dev we're running off the local sqlite3 db...
which uses the better-sqlite3 adapter
i dont see where nuxt hub is overriding this and decicing to go straight for the production db on cloudflare
I feel like this situation is problematic, nuxt hub and db0 kinda are colliding here
db0 is primarily focussed on local dbs atm, while nuxt hub is primarily focussed on remote D1 db's
it feels like for now, the best option is to divert back to Turso + drizzle-orm from drizzle
deploy on CF but db over on Turso + a local one using drizzle orm directly and not the integrated drizzle orm in db0 unless u wanna deploy a nightly nuxt build (it requires that experimental flag)
i mean db0 is still a experimental native sql connector for nuxt in the nightly build atm and nuxt hub is in alpha...
I think I have a fix, I'm working on my example repo for a usable example to share. I'm currently implementing the auth functions and later the sessions to see if everything works.
For now I think I understood that the initializeLucia(hubDatabase()) had to be called in each single line of code so that for each line of code a dedicated D1 binding.
I'll happily tag along your work!
Like in the converted middleware :
dang 38 lines just doubled
but yeah, this looks good!
Im not able to launch my nuxt app atm due to the consistent missing D1 bindings error
just replace every
lucia.
by ìnitializeLucia(hubDatabase())`updating the d1 bindings in root of the project or in the d1 folder/wrangler file keeps resetting it too all default values
what kind of auth strategy are you implementing ? I'm going for passkeys
I'm using a pretty default lucia v3 setup with the password/github provider route
i just made the jump from lucia v2 to v3, so its still new too for me
Ok. I'm using it for the first time. I'm also just a hobby dev, so don't trust my code
no worries! im happy i found someone dealing with the same issue xD
I spent my entire weekend on it alrdy
exactly, sometimes I feel that I could have gone on a stack that's less niche lol
ngl.. we're really working on the edge of technology here, no docs, no posts, no pr's nothing
xD
pioneering this stuff as we try to make it work
the lucia and oslo passkeys have no docs too
i mean, this is all still fun, im just wondering how upset i will feel when i get to the part where the built in node-crypto doesnt work on cf/vercel. this affects everything including oslo and arctic too
without their edge version of it
for hashing the passwords or generating unique-ids
like vercel offers this @vercel/crypto module, u need to apply globally so auth works on their platform..
cf has the same issue, same route solution
didn't know. I guess we have to fork lucia and oslo then
Using Crypto in Edge Middleware and Edge Functions – Vercel
Learn to utilize the crypto Web APIs at the edge.
wrong link
on CF it seems to be supported
https://developers.cloudflare.com/workers/runtime-apis/web-crypto/
Cloudflare Docs
Web Crypto · Cloudflare Workers docs
A set of low-level functions for common cryptographic tasks.
https://discord.com/channels/1004048134218981416/1210630859019919430
heres the discussion on the lucia discord
I might just deploy this to CF, turso in production for now and local sqlite for goofing around during dev
until you have a magic fix in your cloud reproduction repo 😛
On Youtube Mannil said he'll do a video about that.
ill be on the look out for that
GitHub
GitHub - justserdar/jsd-nuxt-turso-drizzle at nightly+lucia
Contribute to justserdar/jsd-nuxt-turso-drizzle development by creating an account on GitHub.
this is my nightly build using experimental features with db0 directly with turso w lucia v3
for production and local dev db in the .data folder
this shud only leave the issues with the differing node:crypto environments on cf
thanks, I'll look at it when production fails and I contemplate my new challenge xD
i got two articles to help you through if you feel lost
https://justserdar.dev/foundry/article/nuxt-experimental-features-connect-your-turso-db-natively-with-db0-and-drizzle
experimental route ^ (w lucia v3)
https://justserdar.dev/foundry/article/connect-a-turso-db-to-your-nuxt-3-project-with-drizzle
normal route ^ (doesnt include lucia)
@manniL / TheAlexLichter sorry to ping you, would love your attention and opinion on this matter, heard you are planning a new video surrounding the subject we're discussing. please feel free to come back at any time, dont wanna inconvencience you
might take a while though 😄
Could cover that while building the association manager
Id be watching it for sure!
pretty sure w many others too
i can understand haha xD
Hey @h+
Sorry for the delay, was a bit busy recently.
I have a working local example :
https://github.com/rktmatt/nuxthub-cloudflare-lucia
On my way to check it deployed on cloudflare and see if the crypto part fails
can't deploy it successfully.
https://2c2f88ba.help-drizzle-nuxt-hub.pages.dev/
I have a lucia + D1 + drizzle setup (without nuxthub) that is working and deployed on CF
GitHub
GitHub - tlebeitsuk/nuxt-cloudflare-lucia: A demo using Nuxt, Cloud...
A demo using Nuxt, Cloudflare Pages, Cloudflare D1 database and Lucia for auth - tlebeitsuk/nuxt-cloudflare-lucia
it looks like the crypto functions are working out of the box.
Thanks for your page, it helped me making the deployment work !
My test of nuxthub cloudflare lucia and passkeys auth is working in prod !
I know the pain, glad it helped
thank you a lot broski!!
extremely helpful
hey man, thanks! i recently checked ur cloud environment/repo to get some middleware going
been very insightful
i read the crypto works out of the box, thats such a relief
happy coding!
@h+ Hey just coming back here to let you know I had to add password auth to another hub project and the build did fail as you said because the argon package is not allowed on Workers.
I found a good repo with a rust based argon worker api. That works perfectly on CF to hash and verify passwords. I only had to add a Bearer token auth to it to make it work.
GitHub
GitHub - glotlabs/argon2-cloudflare
Contribute to glotlabs/argon2-cloudflare development by creating an account on GitHub.
absolute legend that you are, ty for coming back on that!!