Skip to content

[backport cloud/1.51] fix(billing): use user-scoped auth header for customer endpoints - #15908

Merged
comfy-pr-bot merged 1 commit into
cloud/1.51from
backport-15891-to-cloud-1.51
Aug 25, 2026
Merged

comfy-pr-bot merged 1 commit into
cloud/1.51from
backport-15891-to-cloud-1.51

Conversation

@comfy-pr-bot

Copy link
Copy Markdown
Member

Backport of #15891 to cloud/1.51

Automatically created by backport workflow.

)

## Why this is needed

Users who sign in with a **Comfy API key** (no Firebase session) cannot
use any user-scoped billing surface: credit balance never loads, credit
purchase fails, and the usage/events feed errors out. The same flows
work fine for Firebase-authenticated users.

## Root cause

The user-scoped `/customers/*` endpoints accept either a Firebase ID
token (`Authorization: Bearer …`) or a Comfy API key (`X-API-KEY`) — the
backend resolves the user identity from whichever credential is
presented. But on the frontend, most call sites requested **only** the
Firebase header:

- `authStore.fetchBalance()` → `getFirebaseAuthHeader()` → returns
`null` for API-key sessions → throws `userNotAuthenticated`
- `authStore.addCredits()` → same
- `customerEventsService.fetchEvents()` → same → "Authentication header
is missing"

Only `createCustomer()` had an inline API-key fallback, so the customer
record could be created, but every subsequent user-scoped call failed.
The credential choice was duplicated ad hoc per call site instead of
being defined once.

## How we change it

Add one auth-header accessor on `authStore` that encodes the credential
rule for user-scoped endpoints, and use it at every user-scoped call
site:

```ts
const getUserAuthHeader = async (): Promise<AuthHeader | null> =>
  currentUser.value === null
    ? useApiKeyAuthStore().getAuthHeader()   // API-key session → X-API-KEY
    : await getFirebaseAuthHeader()          // signed-in session → Firebase token
```

- `fetchBalance`, `addCredits`, `createCustomer`, `accessBillingPortal`,
and `customerEventsService` now all go through `getUserAuthHeader()`.
- No frontend-derived identity or state: the frontend only forwards the
stored credential; the server keeps deciding whose
balance/events/purchase it is.
- Workspace-scoped calls are untouched — this helper is never a
workspace token.

## AS IS

API-key session: the balance/customer calls fail with a missing auth
header, so the app treats the session as unauthenticated and bounces the
user back to the login dialog.

![AS IS — API-key session bounced to
login](https://raw.githubusercontent.com/dante01yoon/ComfyUI_frontend/assets/pr-15891-screenshots/docs-assets/pr-15891/as-is-api-key-auth.png)

## TO BE

Same API-key session: user popover renders as authenticated and the
credit balance loads (served by the backend from the `X-API-KEY`
credential).

![TO BE — API-key session authenticated with balance
loaded](https://raw.githubusercontent.com/dante01yoon/ComfyUI_frontend/assets/pr-15891-screenshots/docs-assets/pr-15891/to-be-api-key-auth.png)

## Tests

- New unit tests: API-key session sends `X-API-KEY` for `fetchBalance` /
`initiateCreditPurchase`, Firebase token still preferred when signed in,
and `userNotAuthenticated` is thrown when neither credential exists
(`src/stores/authStore.test.ts`).
- `customerEventsService.test.ts` updated to cover the shared accessor.
- `accessBillingPortal` covered both ways: an API-key session sends
`X-API-KEY` to `POST /customers/billing`, and `userNotAuthenticated` is
still thrown when neither credential exists. Without it, Invoice History
and Manage billing in the Credits panel stayed unreachable for API-key
sessions even though the backend accepts the key there.
- 127/127 tests pass across both touched suites.
@comfy-pr-bot comfy-pr-bot added the backport Backporting a PR onto a release candidate label Aug 25, 2026
@comfy-pr-bot
comfy-pr-bot enabled auto-merge (squash) August 25, 2026 17:37
@dosubot dosubot Bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Aug 25, 2026
@github-actions

github-actions Bot commented Aug 25, 2026 •

Copy link
Copy Markdown
Contributor

🎨 Storybook: ✅ Built — View Storybook

Details

⏰ Completed at: 08/25/2026, 05:39:32 PM UTC

Links

🎭 Playwright: ✅ 1830 passed, 0 failed · 1 flaky

📊 Browser Reports
  • chromium: View Report (✅ 1809 / ❌ 0 / ⚠️ 1 / ⏭️ 5)
  • chromium-2x: View Report (✅ 2 / ❌ 0 / ⚠️ 0 / ⏭️ 0)
  • chromium-0.5x: View Report (✅ 1 / ❌ 0 / ⚠️ 0 / ⏭️ 0)
  • mobile-chrome: View Report (✅ 18 / ❌ 0 / ⚠️ 0 / ⏭️ 0)

@github-actions github-actions Bot added the risk:R3 PR risk grade (advisory shadow check; grader-owned) label Aug 25, 2026
@github-actions
github-actions Bot requested a review from thedatalife August 25, 2026 17:38
@codecov

codecov Bot commented Aug 25, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.58824% with 8 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/services/customerEventsService.ts 82.85% 6 Missing ⚠️
src/stores/authStore.ts 96.00% 2 Missing ⚠️
@@              Coverage Diff              @@
##             cloud/1.51   #15908   +/-   ##
=============================================
  Coverage              ?   81.59%           
=============================================
  Files                 ?     1885           
  Lines                 ?   117831           
  Branches              ?    37278           
=============================================
  Hits                  ?    96145           
  Misses                ?    21215           
  Partials              ?      471           
Flag Coverage Δ
unit 73.20% <90.58%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/stores/authStore.ts 82.99% <96.00%> (ø)
src/services/customerEventsService.ts 93.33% <82.85%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@comfy-pr-bot
comfy-pr-bot merged commit 756cef7 into cloud/1.51 Aug 25, 2026
86 of 88 checks passed
@comfy-pr-bot
comfy-pr-bot deleted the backport-15891-to-cloud-1.51 branch August 25, 2026 17:51
@huang47 huang47 added the risk:xhigh PR risk grade (advisory shadow check; grader-owned) label Sep 2, 2026 — with ChatGPT Codex Connector
@huang47 huang47 removed the risk:R3 PR risk grade (advisory shadow check; grader-owned) label Sep 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport Backporting a PR onto a release candidate risk:xhigh PR risk grade (advisory shadow check; grader-owned) size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants