Skip to content

Fix billing user status bug: users with credits now remain active#201

Closed
Josh-XT wants to merge 1 commit intomainfrom
fix-billing-user-status
Closed

Fix billing user status bug: users with credits now remain active#201
Josh-XT wants to merge 1 commit intomainfrom
fix-billing-user-status

Conversation

@Josh-XT
Copy link
Contributor

@Josh-XT Josh-XT commented Feb 18, 2026

Summary

This PR fixes a critical bug where users who have been issued credits were incorrectly showing as inactive and unable to log in.

Problem

Users who received credits were being incorrectly redirected to the subscription page and couldn't access the application. The root cause was in the middleware's handling of HTTP 402 (Payment Required) responses:

  1. Missing credit check: The middleware redirected ALL 402 responses to the subscribe page without checking if the user actually had active credits (input_tokens, output_tokens, or is_active flag).

  2. Unsafe property access: The code assumed responseJSON.detail.customer_session always existed, which could cause runtime errors when the response structure differed after credit issuance.

Solution

Modified the 402 response handling in middleware.tsx to:

  1. Check for active credits: Before redirecting to the subscribe page, the middleware now checks if the user has:

    • input_tokens > 0
    • output_tokens > 0
    • is_active === true
  2. Use safe property access: Added optional chaining (?.) for accessing customer_session.client_secret to prevent runtime errors.

  3. Allow active users through: Users with active credits are no longer redirected to the subscribe page - they proceed as if they received a 200 response.

Changes

  • middleware.tsx: Updated 402 handling logic with credit checks and safe property access

Testing

  • Lint check passes
  • Build completes successfully
  • No new TypeScript errors introduced

How to Test

  1. Simulate a user account with issued credits
  2. Attempt to log in
  3. Verify user can access the application without being redirected to subscribe page

Impact

  • Fixes: Users with credits can now log in and access the application
  • Safe: No breaking changes for users without credits - they will still be properly redirected to subscribe
  • Minimal: Single file change with backwards-compatible logic

… shown as inactive

This fix addresses a critical bug where users who have been issued credits were
showing as inactive and unable to log in.

Changes:
- Modified 402 (Payment Required) handling in middleware to check if user has
  active credits (input_tokens > 0 or output_tokens > 0 or is_active === true)
  before redirecting to subscribe page
- Added safe property access using optional chaining for customer_session to
  prevent runtime errors when response structure differs
- Users with active credits will no longer be incorrectly redirected to the
  subscribe page

The root cause was that the middleware was redirecting ALL 402 responses to
the subscribe page without checking if the user actually had active credits.
Additionally, the code assumed responseJSON.detail.customer_session always
existed, which could cause runtime errors.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@Josh-XT Josh-XT closed this Feb 18, 2026
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