Skip to content

fix(web): 2FA 成功后不再被回跳 /login(ONE-579) - #161

Merged
mapxn merged 1 commit into
mainfrom
agent/mary/f8f833c8d050
Oct 27, 2026
Merged

mapxn merged 1 commit into
mainfrom
agent/mary/f8f833c8d050

Conversation

@mapxn

@mapxn mapxn commented Oct 27, 2026

Copy link
Copy Markdown
Contributor

背景

ONE-579:密码步通过后进入 2FA,TOTP / Passkey 均「验证成功却静默回到未登录页」。stage 1 定位单 ONE-580 已验收确认:服务端全链路 200(/2fa/*/verify 200 → /me 200),根因是前端 TwoFactorPage 的跳转时序竞争,服务端零改动。

根因

apps/web/src/pages/auth/TwoFactorPage.tsx 的 handleSuccess:

navigate(next ?? '/dashboard', { replace: true });
clearPendingState();

BrowserRouter 把路由状态更新包在 React.startTransition(非紧急 lane)里,而 clearPendingState() 是紧急更新。紧急重渲染时 location 仍停在 /two-factor、pendingToken 已变 null,于是本页渲染出 <Navigate to="/login" replace />,约 1ms 后覆盖掉刚跳的 /dashboard。TOTP / 邮箱 OTP / Passkey 共用同一个 handleSuccess,所以两条密码学路径同症状。

改动

  • TwoFactorPage.tsx:用 useRef 记住本页曾持有的 pendingToken,守卫改为「从未有过 token 才回跳 /login」;clearPendingState() 保持原位。零时序假设(不依赖 lane 顺序)。
  • 新增 apps/web/src/pages/auth/TwoFactorPage.dom.test.tsx:jsdom + React 19 + 真实 MemoryRouter(与 BrowserRouter 一样走 startTransition)渲染真实页面,只 stub fetch 与 WebAuthn 仪式。覆盖 4 条:守卫仍生效(无 token 直开 → /login)、pendingToken 丢失后不再渲染守卫、TOTP 落到 /dashboard、Passkey 落到 /dashboard。
  • vite.config.ts + package.json:接入 vitest(test 脚本 = 既有 node --test + vitest run),jsdom/vitest 为 devDependencies。CI 的 pnpm test 现在会跑这层 DOM 测试。

接口契约未变;未改 apps/worker;未碰密钥 / D1。

验证

pnpm check      # biome, 112 files clean
pnpm typecheck  # @hop/web + @hop/worker ok
pnpm build      # ok
pnpm test       # 55 node tests + 4 DOM tests, 全部通过

反向验证:把守卫改回 if (!pendingToken) 后,DOM 测试 3 条失败(最终 location = /login),即复现用户症状;恢复修复后全绿。

细节与 evidence 见 ONE-581 交付评论。

…rify (ONE-579)

TwoFactorPage.handleSuccess navigated to /dashboard and then cleared the
pending state. BrowserRouter commits route state inside
React.startTransition while clearPendingState() is an urgent update, so the
page re-rendered with pendingToken=null while location was still
/two-factor; its <Navigate to="/login"> guard then replaced the fresh
/dashboard entry ~1ms later. Verification itself always succeeded, which is
why the failure looked silent and hit TOTP and Passkey alike.

Retain the token the page arrived with (useRef) and use it for the guard, so
the guard only fires when this page never held a token (direct visit / stale
tab). No API, server or key changes.

Adds the DOM layer the suite was missing: jsdom + React 19 + vitest rendering
the real page in a real MemoryRouter, covering the guard, TOTP and Passkey
landing. All three fail on /login without the fix.
@mapxn
mapxn merged commit 67fd643 into main Oct 27, 2026
2 checks passed
@mapxn
mapxn deleted the agent/mary/f8f833c8d050 branch November 8, 2026 11:29
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