nuxt-hub / core

Build full-stack applications with Nuxt on CloudFlare, with zero configuration.
https://hub.nuxt.com
Apache License 2.0
992 stars 57 forks source link

Missing DB Binding (D1) #224

Closed michaelsieminski closed 3 months ago

michaelsieminski commented 3 months ago

Describe the bug I am using NuxtHub DB with Drizzle and Lucia Auth and did everything like in the docs. It seems to work locally but when deploying im getting the following Error:

12:51:08.792 Error: Failed to publish your Function. Got error: Uncaught Error: Missing Cloudflare DB binding (D1)
  at chunks/runtime.mjs:1:61584 in createError$1
  at chunks/runtime.mjs:1:130393 in hubDatabase
  at chunks/runtime.mjs:13:21305 in useDrizzle
  at chunks/runtime.mjs:13:26554

I am also locally using the --remote flag so it uses the remote database from Cloudflare already but it still seems to break on Deployment

Reproduction link from someone who also had the issue: https://stackblitz.com/~/github.com/rktmatt/help-drizzle-nuxt-hub

Expected behavior It should work and not have missing cloudflare DB bindings

atinux commented 3 months ago

Please do not use server middleware that throw error like this:

if (event.method !== 'GET') {
    const originHeader = getHeader(event, 'Origin') ?? null
    const hostHeader = getHeader(event, 'Host') ?? null
    if (
      !originHeader ||
      !hostHeader ||
      !verifyRequestOrigin(originHeader, [hostHeader])
    ) {
      throw createError({ statusCode: 403 })
    }
  }

This will prevent NuxtHub remote storage to work.

michaelsieminski commented 3 months ago

Please do not use server middleware that throw error like this:

if (event.method !== 'GET') {
    const originHeader = getHeader(event, 'Origin') ?? null
    const hostHeader = getHeader(event, 'Host') ?? null
    if (
      !originHeader ||
      !hostHeader ||
      !verifyRequestOrigin(originHeader, [hostHeader])
    ) {
      throw createError({ statusCode: 403 })
    }
  }

This will prevent NuxtHub remote storage to work.

Oh okay thank you! Is there an alternative for Nuxt to handle CSRF Protection?

I have removed this part but it seems like the same error is being thrown still

atinux commented 3 months ago

Did you deploy using the nuxthub deploy command or Cloudflare Pages CI or do you self-host?

michaelsieminski commented 3 months ago

Did you deploy using the nuxthub deploy command or Cloudflare Pages CI or do you self-host?

I am using nuxthub deploy

atinux commented 3 months ago

Solved on Discord.

The solution is to avoid using useDatabase() outside of the server eventHandlers.

So if you use useDatabase() in a server util, it should be wrapper into a function (ex: useLucia())