API Request errors for better-auth on Cloudflare Workers

Hey! Has someone gotten better-auth to work on cloudflare workers? I am trying to deploy a Nuxt 4 application (via NuxtHub) to cloudflare workers, deployment works, but better-auth fails on requests to API endpoints, despite it working flawlessly in development, so this must be cloudflare-specific issue. I consistently get: CLIENT ERROR: POST https://app.netzo.dev/api/auth/sign-up/email 500 (Internal Server Error) SERVER ERROR: SERVER_ERROR: TypeError: Cannot read properties of undefined (reading 'includes') I am using better-auth-nuxt, I feel this might be related to missing ENV VARS in production, can anyone share any insight? I've tried everything which comes to mind already to no avail. I am setting DATABASE_URL to Supabase's direct connection string, which worked previously when deploying to Cloudflare Pages, but same deployment to Cloudflare Workers (via nuxt/nitro preset cloudflare-durable is failing despite the env variable being properly set in the cloudflare console.
3 Replies
Risatoga
Risatoga4mo ago
same here... But I see this has ben here for a while... @mrk did you manage to find a solution?
mrk
mrkOP3mo ago
Hey! Yes @Risatoga , the following is working for me, truncated:
import { betterAuth } from 'better-auth'
import { drizzleAdapter } from 'better-auth/adapters/drizzle'
import * as schema from '../database/schema'
import { useDB } from './db'

export const serverAuth = () => {
const {
NUXT_PUBLIC_SITE_URL,
NUXT_BETTER_AUTH_SECRET,
NUXT_HUB_ENV
} = process.env
const url = new URL(NUXT_PUBLIC_SITE_URL!)

return betterAuth({
baseURL: url.href,
trustedOrigins: [url.href],
secret: NUXT_BETTER_AUTH_SECRET!,
database: drizzleAdapter(useDB(), { schema, provider: 'pg', usePlural: true }),
// ...
})
}
import { betterAuth } from 'better-auth'
import { drizzleAdapter } from 'better-auth/adapters/drizzle'
import * as schema from '../database/schema'
import { useDB } from './db'

export const serverAuth = () => {
const {
NUXT_PUBLIC_SITE_URL,
NUXT_BETTER_AUTH_SECRET,
NUXT_HUB_ENV
} = process.env
const url = new URL(NUXT_PUBLIC_SITE_URL!)

return betterAuth({
baseURL: url.href,
trustedOrigins: [url.href],
secret: NUXT_BETTER_AUTH_SECRET!,
database: drizzleAdapter(useDB(), { schema, provider: 'pg', usePlural: true }),
// ...
})
}
and then in a separate server/utils/auth.cli.ts required to run bunx @better-auth/cli@latest:
import { serverAuth } from './auth'
export const auth = serverAuth()
import { serverAuth } from './auth'
export const auth = serverAuth()
Risatoga
Risatoga3mo ago
thanks!

Did you find this page helpful?