Skip to content

Throw a clear error when a passkey request is redirected instead of returning JSON - #23

Open
Fuitad wants to merge 1 commit into
laravel:mainfrom
Fuitad:fix/redirect-non-json-response
Open

Throw a clear error when a passkey request is redirected instead of returning JSON#23
Fuitad wants to merge 1 commit into
laravel:mainfrom
Fuitad:fix/redirect-non-json-response

Conversation

@Fuitad

@Fuitad Fuitad commented Jun 16, 2026

Copy link
Copy Markdown

Problem

get/post in src/http.ts only guard !response.ok before calling response.json(). The passkey routes are guest-only and fetch follows 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's RedirectIfAuthenticated returns a 302 to an HTML page. fetch follows it, so response.ok is true (200) but the body is HTML, and response.json() throws the cryptic:

Unexpected token '<', "<!DOCTYPE "... is not valid JSON

which surfaces to the user as a PasskeyError carrying that message instead of anything actionable.

Fix

Reject a followed redirect (response.redirected) on the success path with a clear, actionable message, in both get and post.

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 200 and 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 get and a post test for the redirected-response case. npm run typecheck, npm run lint, prettier --check, and the full suite (74 tests) pass.

…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.
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