whatanerd
whatanerd2mo ago

Can't get better-auth working with the Tanstack Start and Convex stack

The weird part is the error gives me to nothing to work with, just fetch failed and even weirder is that it wouldn't happen on every reload, but when the page does reload, client interactivity wouldn't work, meaning no onClick handler stuff is working. Neither server nor browser console tells me anything. Have followed, checked and rechecked the setup process. https://convex-better-auth.netlify.app/framework-guides/tanstack-start
TanStack Start | Convex + Better Auth
Install and configure Convex + Better Auth for TanStack Start.
No description
41 Replies
whatanerd
whatanerdOP2mo ago
Update: Every single call to the api/auth/$.ts path gives 500 internal server error.
Volko
Volko2mo ago
I'm sorry I don't know tanstack
erquhart
erquhart2mo ago
What version of TanStack Start are you using
whatanerd
whatanerdOP2mo ago
Used better-t-stack to scaffold the project. "@tanstack/react-start": "^1.121.0-alpha.27", I have the updated the necessary packages to their latest versions, and Tanstack Start is now on RC version. Saw your comment over at Github that you're updating the library, docs and example to the RC. Is it still being worked on or live?
erquhart
erquhart2mo ago
not live yet
alfredomm
alfredomm2mo ago
it happens in Node because when the route is built, it uses the WebAPI Request and undici, the default HTTP client in Node, doesn't like that. After the Vercel CEO fiasco I finally moved to Cloudflare and to be done with it
Sara
Sara2mo ago
Hi there, I tested this out last night and it give me the same error in development, do you have it running on local host too? If so, do you mind sharing steps on how to solve this? I've explained some here, https://discord.com/channels/1019350475847499849/1423276934787366963 I've also tested it with social and email sign in, My environment variable are correct as far as i can tell, but for both cases i get dom exception
alfredomm
alfredomm2mo ago
If you wrap that in a try catch you will see that the error comes from undici. I don't remember exactly what the error was, but it had to do with the different implementation of Request in undici compared to the WebAPI. What I did was to just copy the code in reactStartHandler and modified to work with undici/Node so if you're in a Node environment, as you probably are, just import Request from undici and modify the code accordingly
Sara
Sara2mo ago
Yeah it's an error 500 regarding some dom exception, and how did you update it to that? Oh! Okay i will try that today Thank you
alfredomm
alfredomm2mo ago
import { createFileRoute } from '@tanstack/solid-router'

const convexSiteUrl =
import.meta.env.PUBLIC_CONVEX_SITE_URL! ||
process.env.PUBLIC_CONVEX_SITE_URL! ||
import.meta.env.VITE_CONVEX_SITE_URL! ||
process.env.VITE_CONVEX_SITE_URL!

async function handler({ request }: { request: Request }) {
try {
const requestUrl = new URL(request.url)

if (!convexSiteUrl) {
throw new Error('VITE_CONVEX_SITE_URL is not set')
}
const nextUrl = `${convexSiteUrl}${requestUrl.pathname}${requestUrl.search}`

const newRequest = new Request(request, { redirect: 'manual' })
newRequest.headers.set('accept-encoding', 'application/json')

const response = await fetch(nextUrl, newRequest)

const body = response.body
const status = response.status
const statusText = response.statusText

const headers = new Headers(response.headers)

// Ensure no caching
headers.set('Cache-Control', 'no-cache, no-store, must-revalidate')
headers.set('Pragma', 'no-cache')
headers.set('Expires', '0')

return new Response(body, {
status,
statusText,
headers,
})
} catch (e: any) {
console.error(e)

let message = 'Internal Server Error'

if (import.meta.env.MODE !== 'production') {
message = e?.message || message
}

return new Response(message, { status: 500 })
}
}

export const Route = createFileRoute('/api/auth/$')({
server: {
handlers: {
GET: handler,
POST: handler,
},
},
})
import { createFileRoute } from '@tanstack/solid-router'

const convexSiteUrl =
import.meta.env.PUBLIC_CONVEX_SITE_URL! ||
process.env.PUBLIC_CONVEX_SITE_URL! ||
import.meta.env.VITE_CONVEX_SITE_URL! ||
process.env.VITE_CONVEX_SITE_URL!

async function handler({ request }: { request: Request }) {
try {
const requestUrl = new URL(request.url)

if (!convexSiteUrl) {
throw new Error('VITE_CONVEX_SITE_URL is not set')
}
const nextUrl = `${convexSiteUrl}${requestUrl.pathname}${requestUrl.search}`

const newRequest = new Request(request, { redirect: 'manual' })
newRequest.headers.set('accept-encoding', 'application/json')

const response = await fetch(nextUrl, newRequest)

const body = response.body
const status = response.status
const statusText = response.statusText

const headers = new Headers(response.headers)

// Ensure no caching
headers.set('Cache-Control', 'no-cache, no-store, must-revalidate')
headers.set('Pragma', 'no-cache')
headers.set('Expires', '0')

return new Response(body, {
status,
statusText,
headers,
})
} catch (e: any) {
console.error(e)

let message = 'Internal Server Error'

if (import.meta.env.MODE !== 'production') {
message = e?.message || message
}

return new Response(message, { status: 500 })
}
}

export const Route = createFileRoute('/api/auth/$')({
server: {
handlers: {
GET: handler,
POST: handler,
},
},
})
this is my current code, but it's for cloudflare workers instead of using Request directly, you would have to import it from unidici and modify it to fit your needs
Sara
Sara2mo ago
Let me run this and I'll get back to you
whatanerd
whatanerdOP2mo ago
though my runtime is bun for the dev env. I do plan to deploy my application on the cloudflare workers as well. After updating to RC, TS is screaming at me, seems a lot of schema has been changed, so will wait for the docs to get updated prolly.
Sara
Sara2mo ago
it was still erroring even after that update, but this guy over here made it work somehow, I'm getting 404 now, but at least its not a 500 with no context, https://github.com/get-convex/better-auth/issues/118
GitHub
Bug Report: reactStartHandler fails with AbortError due to missing ...
Environment Package: @convex-dev/better-auth@0.8.6 Framework: TanStack Start ^1.132.27 Node.js: ^20.19.9 (with modern Fetch API) Runtime: Node.js server-side handlers Description The reactStartHand...
Volko
Volko2mo ago
I managed to solve mine like this thanks to the OP and AI https://github.com/get-convex/better-auth/issues/118#issuecomment-3369132822
whatanerd
whatanerdOP2mo ago
The better auth library not being compatible with Tanstack RC is my current barrier rn. does your convexClient() fn gives error in plugin?
Volko
Volko2mo ago
Not Thanks to the patch I made thanks to claude and put on the github now it's working I got some issues with the detection of the fact I'm signed in but now it's working
whatanerd
whatanerdOP2mo ago
could you share the final code of it, code and screenshots look a little confusing, just checked, if its not too much work
whatanerd
whatanerdOP5w ago
I have made the necessary migration changes for 0.9, but get this error when any route is loaded
whatanerd
whatanerdOP5w ago
No description
erquhart
erquhart5w ago
Check your router.tsx and make sure it's exporting a function called getRouter https://convex-better-auth.netlify.app/framework-guides/tanstack-start#add-route-context That error looks like TanStack isn't finding that function where it expects it. Oh hmm the example app has src/router.tsx, but the docs have src/routes/router.tsx, maybe that's it
whatanerd
whatanerdOP5w ago
hmm, I did compare my code with the docs one, and indeed getRouter() function wasn't being exported. While that has been taken care of, another unhelpful erroir is being thrown now. I am literally going to cry
No description
erquhart
erquhart5w ago
If there are one or more places where your implementation doesn't match what's documented, you're going to have some issues getWebRequest isn't mentioned anywhere in the docs or examples currently Did you go from 0.7 to 0.9 or were you already on 0.8? The 0.8 guide mentions to go through the last few steps of the tanstack guide to cover updates specific to tanstack: https://convex-better-auth.netlify.app/migrations/migrate-to-0-8#framework-specific-changes
whatanerd
whatanerdOP5w ago
0.8.4 according to git, but that wouldn't help either cuz it wasn't working at time either, was waiting for 0.9 to release.
erquhart
erquhart5w ago
Gotcha - I would go through the TanStack guide and get everything aligned: https://convex-better-auth.netlify.app/framework-guides/tanstack-start
TanStack Start | Convex + Better Auth
Install and configure Convex + Better Auth for TanStack Start.
whatanerd
whatanerdOP5w ago
I honestly am clueless now, till last night it was entirely different shit. I even tracked the getWebRequest part and may have fixed it. But, back to square one, getting the fetch failed error due to the same undici related stuff
whatanerd
whatanerdOP5w ago
Went through the guide so many times, not sure where the discrepancy is...
No description
erquhart
erquhart5w ago
The private property errors generally mean you have multiple copies of the convex dependency. What package manager are you using?
whatanerd
whatanerdOP5w ago
bun: it is in a monorepo architecture
erquhart
erquhart5w ago
Yeah bun would have been my guess. Look for multiple convex dependencies and see if they're conflicting. oh yeah it's right there in the error, convex is installed as a dependency in both the root and apps/web you'll want to limit it to just installing in apps/web and wherever your backend is, probably packages/backend
whatanerd
whatanerdOP5w ago
huh, that's weird since I used Better-T-Stack to scaffold the project. Wasn't even sure about this.
erquhart
erquhart5w ago
Try pnpm instead of Bun. I think Bun is great, but I've seen a fair amount of esoteric issues come through that are Bun specific.
whatanerd
whatanerdOP5w ago
yeah… I think too, though I think changing the runtime and package manager will add another overhead. I think I should’ve gone with Astro, and then used React inside. Way too many new technologies in one stack got me overwhelmed
erquhart
erquhart5w ago
I really think Bun is your only issue here, fwiw Obviously can't be certain, but that's my suspicion But yeah, definitely make the moves you need to make to ship! 🫡
whatanerd
whatanerdOP5w ago
Does the Tanstack plus convex example uses pnpm for its PM? hmm will consider this
erquhart
erquhart5w ago
No, npm for everything, but you're doing monorepo, hence the pnpm recommendation I haven't really seen issues come down to "oh it's pnpm", I've seen many where the culprit is Bun And to be fair to Bun, maybe it's just config errors and not Bun itself. But again, it's the only package manager I've seen a good amount of issues from.
whatanerd
whatanerdOP4w ago
haha fair, will check into this… Hey do we need to explicitly build the core schema that better auth requires, like user, session, account, or the package handles it for us? So, I did scaffold a project using pnpm this time, it scaffolds a todo app, runs fine as long as you are in the homepage, but navigate to dashboard or todos page, and it throws an unhandled, 500 HTTPError
whatanerd
whatanerdOP4w ago
scaffold using pnpm Hey @erquhart I am confused between if I should have two user table, one in betterAuth component and one in app?
erquhart
erquhart4w ago
Most folks go that route
jimpex
jimpex7d ago
I was running into errors but after moving the router.tsx files/changes to the appropriate location it seems to work now 🙏 can't believe the guide led me astray 😔

Did you find this page helpful?