T
TanStack7mo ago
rare-sapphire

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.

server: {
preset: 'cloudflare-pages',
unenv: cloudflare,
},

server: {
preset: 'cloudflare-pages',
unenv: cloudflare,
},
Everything built and deployed as expected.
19:10:36.098 | [success] [vinxi] Nitro Server built
19:10:36.098 | [success] [vinxi] Nitro Server built
When I go to the site and click 'sign in with [provider]' I get a 500 error. The functions logs for /api/auth say:
      "message": [
        "# SERVER_ERROR: ",
        "Error: proxy request failed, cannot connect to the specified address"
      ],
      "message": [
        "# SERVER_ERROR: ",
        "Error: proxy request failed, cannot connect to the specified address"
      ],
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
correct-apricot7mo 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-sapphire
rare-sapphireOP7mo 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
correct-apricot7mo 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-sapphire
rare-sapphireOP7mo 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
correct-apricot7mo 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
No description
correct-apricot
correct-apricot7mo ago
perhaps try email/password auth, that might work since it doesnt use 3rd party APIs for oauth
correct-apricot
correct-apricot7mo ago
for reference, this was how i deployed:
pnpm build
pnpm wrangler pages deploy
pnpm wrangler pages secret bulk .env
pnpm wrangler pages deploy # not sure if a redeploy is necessary for the uploaded env vars
pnpm build
pnpm wrangler pages deploy
pnpm wrangler pages secret bulk .env
pnpm wrangler pages deploy # not sure if a redeploy is necessary for the uploaded env vars
No description
rare-sapphire
rare-sapphireOP7mo 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
correct-apricot7mo 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
name = "cf-tanstarter"
pages_build_output_dir = "./dist"
compatibility_flags = ["nodejs_compat"]
compatibility_date = "2024-11-13"
name = "cf-tanstarter"
pages_build_output_dir = "./dist"
compatibility_flags = ["nodejs_compat"]
compatibility_date = "2024-11-13"
with pnpm wrangler pages deployment tail
Connected to deployment xxxxx, waiting for logs...

POST https://cf-tanstarter.pages.dev/api/auth/sign-in/social - Ok @ 3/9/2025, 11:14:35 PM

GET https://cf-tanstarter.pages.dev/api/auth/callback/discord?code=REDACTED&state=REDACTED - Ok @ 3/9/2025, 11:14:43 PM
(error) # SERVER_ERROR: Error: Cannot perform I/O on behalf of a different request. I/O objects (such as streams, request/response bodies, and others) created in the context of one request handler cannot be accessed from a different request's handler. This is a limitation of Cloudflare Workers which allows us to improve overall performance. (I/O type: Writable)
Connected to deployment xxxxx, waiting for logs...

POST https://cf-tanstarter.pages.dev/api/auth/sign-in/social - Ok @ 3/9/2025, 11:14:35 PM

GET https://cf-tanstarter.pages.dev/api/auth/callback/discord?code=REDACTED&state=REDACTED - Ok @ 3/9/2025, 11:14:43 PM
(error) # SERVER_ERROR: Error: Cannot perform I/O on behalf of a different request. I/O objects (such as streams, request/response bodies, and others) created in the context of one request handler cannot be accessed from a different request's handler. This is a limitation of Cloudflare Workers which allows us to improve overall performance. (I/O type: Writable)
rare-sapphire
rare-sapphireOP7mo 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
correct-apricot7mo 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 neon
rare-sapphire
rare-sapphireOP7mo 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
plain-purple5mo 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 Hyperdrive
Reusing 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:
export const queryClient = postgres(process.env.DATABASE_URL);
export const db = drizzle({
client: queryClient,
schema,
logger: false,
casing: "snake_case",
});
export const queryClient = postgres(process.env.DATABASE_URL);
export const db = drizzle({
client: queryClient,
schema,
logger: false,
casing: "snake_case",
});
I get this error:
Error: Cannot perform I/O o...

Did you find this page helpful?