Cloudflare deployment with better auth issues
Setup with basic environment variables and bun using this template (https://github.com/dotnize/tanstarter). Deploying to cloudflare pages using the presets from Tanstack Start hosting documentation.
Everything built and deployed as expected.
When I go to the site and click 'sign in with [provider]' I get a 500 error. The functions logs for
/api/auth
say:
Which seems that the endpoint is not there/not being deployed to functions.
It doesn't seem to be any issue with environment variables or build issues that I have seen in the discord.
It was recommended to use this setup (https://github.com/nekochan0122/tanstack-start-with-cf-pages-env-vars/blob/main/app.config.ts) but that seems counter to the direction required in the docs. I am wondering if anyone has more up to date information on deploying to Cloudflare.GitHub
tanstack-start-with-cf-pages-env-vars/app.config.ts at main · nekoc...
Contribute to nekochan0122/tanstack-start-with-cf-pages-env-vars development by creating an account on GitHub.
Hosting | TanStack Start React Docs
Hosting is the process of deploying your application to the internet so that users can access it. This is a critical part of any web development project, ensuring your application is available to the...
13 Replies
correct-apricot•7mo ago
i wonder if this is an issue with Start or just how Better Auth is configured. have you tried visiting the endpoint yourself (e.g. https://example.com/api/auth/error) or checked other custom API routes you might have if they also don't work?
rare-sapphireOP•7mo ago
You're right - it does seem to be the Better Auth implementation when deployed. I quickly added a custom API route implemented similar to the Better Auth routes and that returned as expected. Wonder if it actually is something with the environment variables then not allowing Better Auth to init properly
correct-apricot•7mo ago
gave it a try using the preset from docs, with discord oauth:
https://cf-tanstarter.pages.dev/
https://github.com/dotnize/cf-tanstarter
rare-sapphireOP•7mo ago
I didn't fully log in but it seemed to fire the call to the API correctly. I apparently have something misconfigured with the evnironment variables
correct-apricot•7mo ago
yeah this is weird, the callback API route returns error 500 during the oauth flow. but reloading it with the same code & state works and finishes the auth flow

correct-apricot•7mo ago
perhaps try email/password auth, that might work since it doesnt use 3rd party APIs for oauth
correct-apricot•7mo ago
for reference, this was how i deployed:

rare-sapphireOP•7mo ago
I recreated the environment variables and was able to recreate this error in my repo
Would you mind sharing your wrangler.toml (blurred out secrets obviously), just so I have for reference
correct-apricot•7mo ago
exactly the same as the one in the official docs, i never actually tried running it locally so there are no env vars here, only in prod
with
pnpm wrangler pages deployment tail
rare-sapphireOP•7mo ago
Good catch. I'll have to look into how to resolve that, thank you
Looks like it has to do with the connecting to the database. When I turned off the database in the betterAuth init the callback works - but obviously then nothing is persisted. I commented out this section and it worked:
database: drizzleAdapter(db, {
provider: "pg",
}),
When I uncomment it - the callback doesn't work again.correct-apricot•7mo ago
ah yes, could be something with the
postgres
driver. i remember there were some issues with postgres.js and node-postgres when being deployed to cloudflare back then. maybe switch to a different db driver or cloudflare D1?
might be related
https://github.com/porsager/postgres/issues/930
https://github.com/porsager/postgres/issues/865
but what we did back then was just switch to @neondatabase/serverless even though our db wasnt hosted on neonrare-sapphireOP•7mo ago
This is what I am currently trying - just building now. I am using neon - not sure I will stick with that or not. Always open for DB hosted ideas.
Switching the driver did fix the issue. Big thanks for the assist. Now to learn all things Tanstack coming from Angular 😄
This is what I ended up doing to make local development easy and not having to use the neon proxy
import { neon } from '@neondatabase/serverless';
import { drizzle as drizzleNeon } from 'drizzle-orm/neon-http';
import { drizzle as drizzlePg } from 'drizzle-orm/postgres-js';
import postgres from 'postgres';
import * as schema from './schema';
// Check for Cloudflare Worker environment
const isCloudflare = typeof process === 'undefined';
export const db = isCloudflare
? drizzleNeon(neon(process.env.DATABASE_URL!), { schema })
: drizzlePg(postgres(process.env.DATABASE_URL!), { schema });
plain-purple•5mo ago
Hey sorry to bump this thread but in case someone is facing the same issue with cloudflare, my solution was to build the db on request rather than exporting it
https://www.answeroverflow.com/m/1359541012061622342
I tried with noen and I was facing the issue
Error: Cannot perform I/O on behalf of a different request
Or it seems that you can also use HyperdriveReusing DB instance with process.env - Cloudflare Developers
Hi everyone, now that accessing process.env is possible, when I create a db file with a connection to the db:
I get this error:
Error: Cannot perform I/O o...