fix(auth): prevent infinite fetch recursion and multiple wrapper layers#385
Open
fix(auth): prevent infinite fetch recursion and multiple wrapper layers#385
Conversation
This fixes an issue where the frontend would spam the auth endpoints repeatedly when logged out or when a session expired. 1. Infinite Recursion on 401: The window.fetch wrapper would catch a 401, call tryRefresh(), which then called fetch() again, triggering the wrapper recursively if the refresh also failed. We now use the original fetch for refresh attempts and exclude auth endpoints from auto-refresh logic. 2. Multiple Wrapper Layers: Since useAuth is a hook used by many components, multiple instances were independently wrapping window.fetch. We now store the original fetch globally and ensure wrapping only happens once.
Collaborator
Author
I ran a simplifier agent and got this:
Auth nor React is not my thing so I can't really judge how worth it this is. Sorry to dump it on you. |
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.
This PR fixes the issue where the frontend would hammer the auth endpoints when the user was logged out or a session expired. It actually spammed my terminal so hard (probably several hours) that iTerm2 crashed. First time i've seen that!
Turns out there were two main bugs in the
useAuthhook:window.fetchwrapper was intercepting 401s to trigger a refresh, but the refresh request itself was being intercepted by the same wrapper. If the refresh token was expired (returning a 401), it would just loop infinitely.useAuthis used in ~45 different components, we were potentially stacking dozens of nested wrappers on top ofwindow.fetch.obv this is hacks on top of hacks. Presumably there's another fix that cleans all this up a bit. 🤷