Repository navigation
fix(web): 2FA 成功后不再被回跳 /login(ONE-579) - #161
Merged
Merged
Conversation
…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.
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.
背景
ONE-579:密码步通过后进入 2FA,TOTP / Passkey 均「验证成功却静默回到未登录页」。stage 1 定位单 ONE-580 已验收确认:服务端全链路 200(
/2fa/*/verify200 →/me200),根因是前端TwoFactorPage的跳转时序竞争,服务端零改动。根因
apps/web/src/pages/auth/TwoFactorPage.tsx的handleSuccess: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)渲染真实页面,只 stubfetch与 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。验证
反向验证:把守卫改回
if (!pendingToken)后,DOM 测试 3 条失败(最终 location =/login),即复现用户症状;恢复修复后全绿。细节与 evidence 见 ONE-581 交付评论。