Skip to content

Commit f622f4a

Browse files
donjor-agent[bot]rsi-pr-agent
authored andcommitted
fix(web): pair on same-tab token hash navigation (#1740)
## Problem Navigating an existing `/pair` tab to `/pair#token=...` never submits the token and leaves it in the address bar. ## Change Consume URL tokens on mount and `hashchange`, using the existing token-stripping helper and credential submission path. Remove the obsolete mount-only token refs. ## Scope and approval The host approved this focused fix and PR publication. Closes #146. ## Verification ### Behavior - [x] **Navigation:** same-tab token pairs - [x] **Privacy:** token stripped from URL - [x] **First load:** pairing still succeeds - [x] **Tests:** 2 passed, 0 failed ### Checks - [x] **Types:** web typecheck, exit 0 - [x] **Lint:** changed files, exit 0 - [x] **Build:** web production, exit 0 - [x] **Gate:** `vp run fork:ci`, exit 0 - [ ] **Live evidence:** host grounding pending No servers or browsers launched by this worker. Hosted pairing and native mobile are unchanged. Implementation: Pi, gpt-6.1-sol; host: Claude Code, claude-opus-5-5. ## Fork trailers Squashes: - 8d3c37e fix(web): pair on same-tab token hash navigation (#1740) (#1740) - 46cf384 fixup! fix(web): pair on same-tab token hash navigation (#1740) (#1740) Fork-Domain: upstream-fixes Fork-Tier: bugfix Fork-Upstreamable: yes
1 parent 0313964 commit f622f4a

2 files changed

Lines changed: 72 additions & 11 deletions

File tree

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { StrictMode } from "react";
2+
import { describe, expect, it, vi } from "vite-plus/test";
3+
4+
import { act, render } from "../../../test/render";
5+
import { submitServerAuthCredential } from "../../environments/primary";
6+
import { PairingRouteSurface } from "./PairingRouteSurface";
7+
8+
vi.mock("../../environments/primary", async (importOriginal) => ({
9+
...(await importOriginal<typeof import("../../environments/primary")>()),
10+
submitServerAuthCredential: vi.fn().mockResolvedValue(undefined),
11+
}));
12+
13+
async function renderPairing(url = "/pair") {
14+
vi.mocked(submitServerAuthCredential).mockClear();
15+
window.history.replaceState({}, "", url);
16+
const onAuthenticated = vi.fn();
17+
const screen = await render(
18+
<StrictMode>
19+
<PairingRouteSurface
20+
auth={{
21+
policy: "loopback-browser",
22+
bootstrapMethods: ["one-time-token"],
23+
sessionMethods: ["browser-session-cookie"],
24+
sessionCookieName: "t3_session",
25+
}}
26+
onAuthenticated={onAuthenticated}
27+
/>
28+
</StrictMode>,
29+
{ router: false },
30+
);
31+
return Object.assign(screen, { onAuthenticated });
32+
}
33+
34+
describe("pairing URL tokens", () => {
35+
it("pairs after same-tab hash navigation and removes the token from the URL", async () => {
36+
await using screen = await renderPairing();
37+
expect(submitServerAuthCredential).not.toHaveBeenCalled();
38+
39+
await act(() => {
40+
window.history.pushState({}, "", "/pair#token=navigation-token");
41+
window.dispatchEvent(new HashChangeEvent("hashchange"));
42+
});
43+
44+
expect(submitServerAuthCredential).toHaveBeenCalledExactlyOnceWith("navigation-token");
45+
expect(screen.onAuthenticated).toHaveBeenCalledTimes(1);
46+
expect(window.location.pathname).toBe("/pair");
47+
expect(window.location.hash).toBe("");
48+
});
49+
50+
it("still pairs on first load and removes the token from the URL", async () => {
51+
await using screen = await renderPairing("/pair#token=initial-token");
52+
53+
expect(submitServerAuthCredential).toHaveBeenCalledExactlyOnceWith("initial-token");
54+
expect(screen.onAuthenticated).toHaveBeenCalledTimes(1);
55+
expect(window.location.hash).toBe("");
56+
});
57+
});

‎apps/web/src/components/auth/PairingRouteSurface.tsx‎

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
peekPairingTokenFromUrl,
99
stripPairingTokenFromUrl,
1010
submitServerAuthCredential,
11+
takePairingTokenFromUrl,
1112
} from "../../environments/primary";
1213
import { readHostedPairingRequest } from "../../hostedPairing";
1314
import { Button } from "../ui/button";
@@ -35,11 +36,9 @@ export function PairingRouteSurface({
3536
initialErrorMessage?: string;
3637
onAuthenticated: () => void;
3738
}) {
38-
const [autoPairToken] = useState(peekPairingTokenFromUrl);
39-
const [credential, setCredential] = useState(autoPairToken ?? "");
39+
const [credential, setCredential] = useState(() => peekPairingTokenFromUrl() ?? "");
4040
const [errorMessage, setErrorMessage] = useState(initialErrorMessage ?? "");
4141
const [isSubmitting, setIsSubmitting] = useState(false);
42-
const autoSubmitAttemptedRef = useRef(false);
4342

4443
const submitCredential = async (nextCredential: string) => {
4544
setIsSubmitting(true);
@@ -68,19 +67,24 @@ export function PairingRouteSurface({
6867
};
6968

7069
const autoSubmit = useEffectEvent((token: string) => {
70+
setCredential(token);
7171
void submitCredential(token);
7272
});
7373

7474
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- strips the pairing token from the URL and submits it once when the route carries one
7575
useEffect(() => {
76-
if (!autoPairToken || autoSubmitAttemptedRef.current) {
77-
return;
78-
}
79-
80-
autoSubmitAttemptedRef.current = true;
81-
stripPairingTokenFromUrl();
82-
autoSubmit(autoPairToken);
83-
}, [autoPairToken]);
76+
const submitUrlToken = () => {
77+
const token = takePairingTokenFromUrl();
78+
if (!token) {
79+
return;
80+
}
81+
autoSubmit(token);
82+
};
83+
84+
window.addEventListener("hashchange", submitUrlToken);
85+
submitUrlToken();
86+
return () => window.removeEventListener("hashchange", submitUrlToken);
87+
}, []);
8488

8589
return (
8690
<StandalonePage>

0 commit comments

Comments
 (0)