Throw a clear error when a passkey request is redirected instead of returning JSON - #23
Open
Fuitad wants to merge 1 commit into
Open
Throw a clear error when a passkey request is redirected instead of returning JSON#23Fuitad wants to merge 1 commit into
Fuitad wants to merge 1 commit into
Conversation
…eturning JSON The get/post helpers only guard !response.ok before calling response.json(). The passkey routes are guest-only and fetch follows redirects by default, so a request made while already authenticated is redirected to an HTML page; fetch follows it, response.ok is true (200), and response.json() throws the cryptic "Unexpected token '<', \"<!DOCTYPE \"... is not valid JSON". Reject a followed redirect (response.redirected) on the success path with a clear, actionable message in both get and post, and cover it with tests.
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.
Problem
get/postinsrc/http.tsonly guard!response.okbefore callingresponse.json(). The passkey routes are guest-only andfetchfollows redirects by default, so when a request is made while the session is already authenticated (for example a back/forward-cached login page, or a second tab), the server'sRedirectIfAuthenticatedreturns a302to an HTML page.fetchfollows it, soresponse.okistrue(200) but the body is HTML, andresponse.json()throws the cryptic:which surfaces to the user as a
PasskeyErrorcarrying that message instead of anything actionable.Fix
Reject a followed redirect (
response.redirected) on the success path with a clear, actionable message, in bothgetandpost.This is intentionally minimal and targeted at the reported scenario (a redirect followed to a non-JSON page). A content-type check could additionally cover a server that returns HTML with a bare
200and no redirect, but that is a separate edge case and would change the existing mocks; happy to extend if you'd prefer that direction.Tests
Adds a
getand aposttest for the redirected-response case.npm run typecheck,npm run lint,prettier --check, and the full suite (74 tests) pass.