Skip to content

fix(templates): support web and native together in Convex Better Auth - #1294

Merged
AmanVarshney01 merged 4 commits into
mainfrom
fix/convex-web-native-auth
Oct 22, 2026
Merged

AmanVarshney01 merged 4 commits into
mainfrom
fix/convex-web-native-auth

Conversation

@AmanVarshney01

@AmanVarshney01 AmanVarshney01 commented Oct 22, 2026 •

Copy link
Copy Markdown
Owner

Summary

Fixes Convex + Better Auth scaffolds that have both a web frontend and a native (Expo) frontend.

  • Better Auth Convex Implementation not working in both web and native #745: with Next.js or TanStack Start plus native, convex/auth.ts pinned baseURL: siteUrl, so native social sign-in sent OAuth callbacks to the web app's origin (for example localhost:3001, which a device can't reach). For this combination, baseURL is now a dynamic config: allowedHosts: [siteUrl, process.env.CONVEX_SITE_URL!], with fallback: siteUrl, and advanced.trustedProxyHeaders: true. Web requests that pass through the Next/Start /api/auth proxy still resolve to the site URL, because the proxy sets x-better-auth-forwarded-host and the component turns it back into x-forwarded-host. Native requests that go straight to Convex resolve to the .convex.site URL. Only those two hosts are accepted. Web-only Next and Start output is unchanged (baseURL: siteUrl).
  • Uncaught (in promise) TypeError: Failed to fetch - React Native | Convex | Better Auth #669: when a web frontend exists, SITE_URL and crossDomain({ siteUrl }) point at the web app, so Expo Web (http://localhost:8081) was neither a trusted origin nor allowed by CORS, which caused "Failed to fetch". Web + native setups now add http://localhost:8081 to trustedOrigins, the same way the non-Convex Better Auth server template already does. Native-only output is unchanged.
  • Added a next-expo-convex-better-auth generated-build sample (Next + native-uniwind).

expectAuth is not added.

Docs followed

  • Convex Better Auth Expo guide (https://labs.convex.dev/better-auth/framework-guides/expo): sets no server baseURL, because the client points at CONVEX_SITE_URL. For Expo Web it says to add the site URL to trustedOrigins, use crossDomain, set cors: true, and pick crossDomainClient or expoClient by platform. The generated native client already follows this.
  • Next.js and TanStack Start guides: baseURL: siteUrl, used through the framework proxy. This is kept for web-only setups.
  • Better Auth options (dynamic baseURL with allowedHosts/fallback, advanced.trustedProxyHeaders) and the Better Auth Expo integration (trustedOrigins with the app scheme and exp://).

Not tested live

I did not run OAuth or session flows against a live Convex deployment. The changes were checked through generation, typechecking, builds, and by reading the @convex-dev/better-auth 0.12.5 and better-auth 1.6.17 source (proxy header forwarding and dynamic base URL resolution).

Verification

  • bun run check: passes
  • cd apps/cli && bun run test:ci: 112 pass, 0 fail
  • BTS_BUILD_SAMPLES=1 BTS_BUILD_SAMPLE_FILTER=convex bun test test/generated-builds.test.ts: 7 pass, 0 fail, including the new sample (install, check-types, build, expo export for iOS and Android)
  • Generated Convex + Better Auth projects with create-json: next + native-uniwind, tanstack-start + native-bare, tanstack-router + native-unistyles, native-only, and next-only. Ran bun install and bun run check-types on each and all pass (the Vite web apps need non-placeholder VITE_CONVEX_* values for varlock). I also read the generated convex/auth.ts for each. next-only still emits baseURL: siteUrl, trustedOrigins: [siteUrl].

Summary by CodeRabbit

  • New Features
    • Added a generated-build sample combining Next.js and Expo Uniwind frontends with Convex, Better Auth, and Turborepo.
    • Improved Better Auth host handling for native frontends paired with Next.js or TanStack Start, including allowed hosts, a fallback URL, and trusted proxy headers.
    • Added localhost:8081 as a trusted origin for supported native frontend combinations, including Next.js, TanStack Start, TanStack Router, React Router, Nuxt, Svelte, and Solid.

Resolve the Better Auth base URL per request when Next.js or TanStack Start ships with a native app, so web requests proxied through the app keep the site URL and native requests sent straight to Convex use the Convex site URL. Trust the Expo Web dev origin when a web frontend also exists.

Fixes #745

Fixes #669
@vercel

vercel Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
create-better-t-stack-web Ready Ready Preview Oct 22, 2026 6:33pm UTC

Request Review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-22T08:08:40.432694Z 5088b4a PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5088b4aad0

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@coderabbitai

coderabbitai Bot commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: c58ef134-2f7e-4855-be2d-1e45da23b998
📥 Commits

Reviewing files that changed from the base of the PR and between 4c20090 and 8ed9254.

⛔ Files ignored due to path filters: 1
  • packages/template-generator/src/templates.generated.ts is excluded by !**/*.generated.*
📒 Files selected for processing: 1
  • apps/cli/test/generated-builds/samples.ts

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 4 remain after this review.

Walkthrough

The change adds a Next.js and Expo Uniwind build sample with Convex and Better Auth. It also updates Better Auth URL and trusted-origin settings for specified native frontend combinations.

Changes

Native Better Auth configuration

Layer / File(s) Summary
Native build sample and auth settings
apps/cli/test/generated-builds/samples.ts, packages/template-generator/templates/auth/better-auth/convex/backend/convex/auth.ts.hbs
Adds a build sample combining Next.js and Expo Uniwind with Convex and Better Auth. For native frontends with TanStack Start or Next.js, baseURL allows siteUrl and CONVEX_SITE_URL, uses siteUrl as the fallback, and enables trusted proxy headers. Selected native frontend configurations add http://localhost:8081 to trustedOrigins.

Priority: ➖ Normal

Merge Risk: ⚪ Minimal · up to 8ed92

The reviewed configuration changes do not present a concrete merge-blocking risk.

Pre-merge checks: Passed 5
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: fixing Convex Better Auth templates for web and native frontend combinations.
Docstring Coverage Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 1…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 1 files.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to GitHub limitations.

⚠️ Outside diff range comments (1)

🟡 Minor · Provision CONVEX_SITE_URL for mixed web/native projects. · auth.ts.hbs:32-38

packages/template-generator/templates/auth/better-auth/convex/backend/convex/auth.ts.hbs:32-38
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Provision CONVEX_SITE_URL for mixed web/native projects.

The generated Better Auth configuration includes process.env.CONVEX_SITE_URL in allowedHosts, but the mixed Next.js/native and TanStack Start/native generator branches do not provision this backend variable. For a native request sent directly to the Convex site, Better Auth 1.6.17 ignores the undefined host pattern, finds no allowed host, and applies fallback: siteUrl. The native request therefore resolves auth URLs to the web site instead of the Convex site.

Add the backend CONVEX_SITE_URL variable and its setup instruction in both affected generator branches.


ℹ️ Review info
⚙️ Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 1eb1551e-d978-4c59-a0cb-763b9d63f020
📥 Commits

Reviewing files that changed from the base of the PR and between 5088b4a and d599309.

⛔ Files ignored due to path filters: 1
  • packages/template-generator/src/templates.generated.ts is excluded by !**/*.generated.*
📒 Files selected for processing: 1
  • apps/cli/test/generated-builds/samples.ts

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 6 remain after this review.

@AmanVarshney01
AmanVarshney01 merged commit f58be65 into main Oct 22, 2026
11 checks passed
@AmanVarshney01
AmanVarshney01 deleted the fix/convex-web-native-auth branch October 22, 2026 18:41

This branch was successfully deployed

1 active deployment
Preview — 8ed9254e Deployed Oct 22, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant