Skip to content

Open source personal finance application

refactor: use env vars for CORS origins instead of hardcoded list

Ilya Komichev8af71593829a
diff --git a/.gitignore b/.gitignore
index cb3ec5f9..9be1de49 100644
--- a/.gitignore
+++ b/.gitignore
@@ -19,6 +19,7 @@ logs
.DS_Store
.fleet
.idea
+.agents
# Local env files
.env
diff --git a/CLAUDE.md b/CLAUDE.md
index 39263e04..69367a36 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -107,14 +107,13 @@ Convex dev deployment env vars must be configured (one-time):
```sh
npx convex env set BETTER_AUTH_SECRET <value>
-npx convex env set SITE_URL https://<dev-deployment>.eu-west-1.convex.site
-npx convex env set FRONTEND_URL http://localhost:3050
+npx convex env set APP_URL http://localhost:3050
npx convex env set GOOGLE_CLIENT_ID <value>
npx convex env set GOOGLE_CLIENT_SECRET <value>
npx convex env set OPENEXCHANGERATES_APP_ID <value>
```
-CORS: dev deployment origins (`https://<deployment>.convex.site` and `https://<deployment>.convex.cloud`) must be listed in `convex/http.ts` `allowedOrigins`.
+CORS is configured automatically from `CONVEX_SITE_URL` (built-in) and `APP_URL` env vars in `convex/http.ts` — no manual origin lists needed.
## Environment Variables
@@ -135,8 +134,8 @@ All `.env*` files are gitignored except `.env.example`.
### Convex backend env vars (set via `npx convex env set`)
- `BETTER_AUTH_SECRET` — secret key for Better Auth (**required** — without it all auth endpoints including CORS preflight will fail)
-- `SITE_URL` — Convex site URL (Better Auth base URL)
-- `FRONTEND_URL` — frontend URL for cross-domain auth
+- `APP_URL` — app URL for cross-domain auth and CORS/trustedOrigins
+- `CONVEX_SITE_URL` — built-in Convex system variable (auto-provided, do not set manually), used as Better Auth base URL and for CORS
- `GOOGLE_CLIENT_ID` / `GOOGLE_CLIENT_SECRET` — Google OAuth
- `OPENEXCHANGERATES_APP_ID` — exchange rates API
diff --git a/README.md b/README.md
index fa9a3df6..7afbd13d 100644
--- a/README.md
+++ b/README.md
@@ -74,8 +74,7 @@ VITE_CONVEX_SITE_URL=your_convex_site_url
These variables run on the Convex backend. Set them via CLI:
```bash
-npx convex env set SITE_URL your_convex_site_url
-npx convex env set FRONTEND_URL https://your-app-domain.com
+npx convex env set APP_URL https://your-app-domain.com
npx convex env set GOOGLE_CLIENT_ID your_client_id
npx convex env set GOOGLE_CLIENT_SECRET your_client_secret
npx convex env set OPENEXCHANGERATES_APP_ID your_app_id
diff --git a/convex/auth.ts b/convex/auth.ts
index 1eabbc03..5dfdbf23 100644
--- a/convex/auth.ts
+++ b/convex/auth.ts
@@ -9,10 +9,9 @@ import type { DataModel } from './_generated/dataModel'
import { components } from './_generated/api'
import authConfig from './auth.config'
-import { frontendOrigins } from './origins'
-const siteUrl = process.env.SITE_URL!
-const frontendUrl = process.env.FRONTEND_URL || 'http://localhost:3050'
+const siteUrl = process.env.CONVEX_SITE_URL!
+const appUrl = process.env.APP_URL || 'http://localhost:3050'
export const authComponent = createClient<DataModel>(components.betterAuth)
@@ -41,7 +40,7 @@ function createAuth(ctx: GenericCtx<DataModel>) {
jwksRotateOnTokenGenerationError: true,
}),
crossDomain({
- siteUrl: frontendUrl,
+ siteUrl: appUrl,
}),
],
session: {
@@ -54,7 +53,7 @@ function createAuth(ctx: GenericCtx<DataModel>) {
clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
},
},
- trustedOrigins: frontendOrigins,
+ trustedOrigins: [appUrl],
})
}
diff --git a/convex/http.ts b/convex/http.ts
index 1c292c41..ca08ad93 100644
--- a/convex/http.ts
+++ b/convex/http.ts
@@ -1,19 +1,19 @@
import { httpRouter } from 'convex/server'
import { authComponent, createAuth } from './auth'
-import { frontendOrigins } from './origins'
+
+const appUrl = process.env.APP_URL || 'http://localhost:3050'
+const siteUrl = process.env.CONVEX_SITE_URL!
const http = httpRouter()
authComponent.registerRoutes(http, createAuth, {
cors: {
allowedOrigins: [
- ...frontendOrigins,
+ appUrl,
// Convex deployment origins (needed for CORS on auth HTTP actions)
- 'https://frugal-hornet-995.eu-west-1.convex.site',
- 'https://frugal-hornet-995.eu-west-1.convex.cloud',
- 'https://proficient-elk-135.eu-west-1.convex.site',
- 'https://proficient-elk-135.eu-west-1.convex.cloud',
+ siteUrl,
+ siteUrl.replace('.convex.site', '.convex.cloud'),
],
},
})
diff --git a/convex/origins.ts b/convex/origins.ts
deleted file mode 100644
index 5e554815..00000000
--- a/convex/origins.ts
+++ /dev/null
@@ -1,6 +0,0 @@
-export const frontendOrigins = [
- 'https://finapp.ilko.me',
- 'https://finapp-git-convex-ilkome2.vercel.app',
- 'https://main-git-convex-ilkome2.vercel.app',
- 'http://localhost:3050',
-]