Skip to content

fix(auth): stop prod magic-link from hitting localhost and missing apikey - #341

Merged
thinkclay merged 2 commits into
mainfrom
fix/sdk-apikey-and-prod-url-leak
Jun 11, 2026
Merged

thinkclay merged 2 commits into
mainfrom
fix/sdk-apikey-and-prod-url-leak

Conversation

@thinkclay

Copy link
Copy Markdown
Contributor

Summary

Fixes two bugs that combined to break magic-link on app.scaffald.com:

  1. Prod bundle had http://127.0.0.1:54321/... baked in. EXPO_PUBLIC_SCAFFALD_API_URL was set in .env (the dev fallback). Expo loads .env before .env.production, and .env.production doesn't define this var, so the localhost value survived into the prod web bundle. Every Scaffald-SDK call from a user's browser was routed to localhost.
  2. Even with the right URL, Supabase Kong rejected every request with 400 {"message":"No API key found in request"} because the SDK only sent Authorization, never the apikey header that Kong requires on /functions/v1/*.

What this PR changes

  • .env.template — adds a prominent warning that EXPO_PUBLIC_SCAFFALD_API_URL should NEVER be set. The fallback in jobs-sdk-context.tsx derives ${SUPABASE_URL}/functions/v1/api, which is correct in every env. (.env is gitignored — devs need to remove the line from their local copy.)
  • packages/scf-core/utils/jobs-sdk-context.tsx — passes EXPO_PUBLIC_SUPABASE_ANON_KEY to the SDK via the new typed anonKey field so HttpClient emits the apikey header on every request.
  • packages/sdk submodule bump → a2bd925 — picks up Scaffald/sdk#6, which adds the typed anonKey field and apikey header emit.

Depends on Scaffald/sdk#6. This PR currently points at the SDK branch commit a2bd925. After Scaffald/sdk#6 merges, re-bump the submodule to the merge commit before merging this PR.

Verification

Built apps/scaffald in prod mode (APP_ENV=production NODE_ENV=production pnpm web:build) after clearing Metro's system cache (/private/var/folders/.../T/metro-cache — see test-plan note below). In the fresh entry-*.js:

$ grep -c 127.0.0.1:54321 dist/_expo/static/js/web/entry-*.js
0
$ grep -oE "https?://auth\.scaffald\.com" dist/_expo/static/js/web/entry-*.js | sort -u
https://auth.scaffald.com
$ grep -oE ".{40}config\.anonKey.{60}" dist/_expo/static/js/web/entry-*.js | head -1
rer ${this.config.supabaseToken}`),this.config.anonKey&&(v.apikey=this.config.anonKey),"POST"===s&&u

✅ 127.0.0.1 gone, prod URL intact, apikey emit present.

Test plan

  • Merge fix(http): send Supabase apikey header when anonKey is configured sdk#6 and re-bump submodule pointer here.
  • Clear Metro's system cache before the prod build (rm -rf /private/var/folders/*/T/metro-*). Without this, Metro retains the previously inlined env var across builds — bit me twice during diagnosis.
  • Build prod (APP_ENV=production NODE_ENV=production pnpm web:build in apps/scaffald).
  • Deploy apps/scaffald/dist to wherever serves app.scaffald.com.
  • Hard-reload app.scaffald.com/auth/login, attempt magic-link.
  • Confirm Network tab: request goes to https://auth.scaffald.com/functions/v1/api/v1/auth/magic-link and carries both Authorization: Bearer … and apikey: … headers.
  • Confirm email arrives.

🤖 Generated with Claude Code

thinkclay and others added 2 commits June 11, 2026 03:22
…ikey

Two distinct bugs combined to break magic-link on app.scaffald.com:

1. `EXPO_PUBLIC_SCAFFALD_API_URL=http://127.0.0.1:54321/...` in `.env`
   leaked into the prod web bundle. Expo loads `.env` before
   `.env.production`, and `.env.production` doesn't define this var, so
   the dev value survived. The Scaffald SDK then routed every call to
   localhost from users' browsers. Fixed by documenting the gotcha
   prominently in `.env.template` (`.env` is gitignored — devs need to
   update their local copy).

2. Even if the URL were right, every request hit Supabase Kong without
   an `apikey` header, which Kong requires on `/functions/v1/*` (it
   returns `400 {"message":"No API key found in request"}` regardless of
   Authorization). Fixed at the SDK level (see Scaffald/sdk#6 —
   adds typed `anonKey` field, HttpClient emits `apikey` header from
   it) and plumbed through `jobs-sdk-context.tsx` from
   `EXPO_PUBLIC_SUPABASE_ANON_KEY`.

Bumps `packages/sdk` to a2bd925 (fix/http-anonkey-apikey-header
branch); re-bump to the merge commit once Scaffald/sdk#6 lands.

Verified against a fresh prod-mode bundle:
- `127.0.0.1:54321` no longer appears in `entry-*.js`.
- `auth.scaffald.com` is the only Supabase host present.
- `this.config.anonKey && (v.apikey = ...)` is in the minified output.

Co-Authored-By: Claude Opus 4.7 <[email protected]>
Picks up the merged Scaffald/sdk#6 — the typed anonKey field and
apikey-header emit are now on main.

Co-Authored-By: Claude Opus 4.7 <[email protected]>
@thinkclay
thinkclay merged commit 3529a21 into main Jun 11, 2026
8 of 11 checks passed
@thinkclay
thinkclay deleted the fix/sdk-apikey-and-prod-url-leak branch June 11, 2026 07:34
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