Fix billing user status bug: users with credits now remain active#201
Closed
Fix billing user status bug: users with credits now remain active#201
Conversation
… 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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
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).
Unsafe property access: The code assumed
responseJSON.detail.customer_sessionalways existed, which could cause runtime errors when the response structure differed after credit issuance.Solution
Modified the 402 response handling in
middleware.tsxto:Check for active credits: Before redirecting to the subscribe page, the middleware now checks if the user has:
input_tokens > 0output_tokens > 0is_active === trueUse safe property access: Added optional chaining (
?.) for accessingcustomer_session.client_secretto prevent runtime errors.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 accessTesting
How to Test
Impact