Enhancement: Improve BillableMiddleware for SPAs | revive PR #167 #437
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.
Note: This is not my original work, its a copy of #167 by @ggelashvili
Overview
This PR enhances the
Billable
middleware to provide better support for SPAs by allowing AJAX requests to receive a JSON response with a redirect URL. Currently,Billable
middleware and thebilling_enabled
configuration have no use for SPAs, & causes confusion which this PR aims to rectify.Changes
The updated
Billable
middleware now checks if the incoming request is an AJAX request and, if billing is enabled, returns a403
status code alongside a JSON response containing aforceRedirectUrl
field. This field provides a URL to which the SPA can redirect the user. The updated middleware assumes that thehost
parameter is included in the request, which can be easily implemented using an axios interceptor (if using axios on front-end).By returning a JSON response and a
403
status code, the front-end can now hook into the response interceptor, catch403
errors, extract the redirect URL from the response, and manage redirection on the front-end. This makes theBillable
middleware more useful for SPAs.I added my own middleware in a project I'm working on with React front-end & worked perfectly, so thought it might be useful to improve the original Billable middleware.
Note that the wiki would need to be updated if this gets approved/merged.
This should not be a breaking change since
Billable
middleware should not be used anyways for traditional SPAs.