Repository navigation
Commit d8b7ef4
fix(auth): a user with two-factor sign-in on is asked for their code after the password (#29338)
<!-- claude-projects-attribution: {"github_login":"zhuangjianguo"} -->
_Requested by **Steedos** · [project
thread](https://claude.ai/code/project/chan_018ETKaNwMZCNRYD7D8Q9Ubb?thread=cmsg_018ETKaNwMZCNRYD7D8Q9UbbRSWiurS43dXzxySsivhDtb)_
Fixes #29105
Clause-②: yes (widening)
Metadata-protocol: none
## 维护者速读
- **改了什么**:开启了双重验证(身份验证器 TOTP)的用户用密码登录后,Console 登录页会出现第二步:输入身份验证器里的 6
位验证码,或改用备用码;验证通过即登录。验证码错误会在原处提示并可重试;"返回登录"回到密码步骤。Console 自己的登录页和
`DefaultLoginPage` 都有这一步,十种语言包都有文案。
- **为什么改**:服务端早已支持两步登录(密码通过后返回 `twoFactorRedirect`,再由
`/two-factor/verify-totp` 或 `/verify-backup-code` 完成),但 Console
把这个回答丢掉了,登录页原样不动、没有输入框也没有任何提示,这类用户完全进不了 Console。
-
**风险与回滚**:只改前端(`@object-ui/auth`、`app-shell`、`i18n`、`console`),服务端零改动;未开启双重验证的用户登录路径不变(有测试钉住)。`@object-ui/auth`
的公开类型新增了成员(拓宽)。回滚即 revert 本 PR。
- **先看**:`ui/packages/auth/src/AuthProvider.tsx`(`signIn`
收到挑战时不登录、保存挑战;`verifySignInTwoFactor`
完成登录,挑战被服务端作废时清掉);`ui/packages/auth/src/TwoFactorChallengeForm.tsx`(新的验证码步骤);`ui/packages/auth/src/createAuthClient.ts`(识别
`twoFactorRedirect`、丢弃那次回答里已被删掉的会话 token)。
- **两分钟验证**:`OS_AUTH_TWO_FACTOR=true pnpm dev:crm -- --fresh -p 38417`,给
admin 开启 TOTP 后在 Console `/login` 用密码登录:出现验证码步骤;输错显示"That code is not
valid…",输对进入 `/home`。
- **需要你做的**:无,席位落地。
## What changes
A password sign-in for an account with two-factor sign-in on is answered
by better-auth's `twoFactor()` plugin with `{ twoFactorRedirect: true,
twoFactorMethods }`, no session, and a signed `two_factor` cookie that
holds the pending sign-in for ten minutes. The Console ignored that
answer. Now:
- **`@object-ui/auth` client.** `AuthClient.signIn` answers `{
twoFactor: { methods } }` for that case (new exported `SignInResult`,
`TwoFactorChallenge`, `TwoFactorMethod`). The same answer carries
`set-auth-token` for the session the server just deleted (measured: it
resolves to no session), which the bearer fetch had stored; the client
clears it. New `AuthClient.verifySignInTwoFactor(code, 'totp' |
'backup-code')` posts to the sign-in lane of `/two-factor/verify-totp`
or `/two-factor/verify-backup-code` and stores the session token it
answers.
- **Provider.** `useAuth()` gains `twoFactorChallenge`,
`verifySignInTwoFactor` and `cancelTwoFactorChallenge`; `signIn`
resolves `{ twoFactorRequired }`. The challenge clears on success, on
"Back to sign in", on sign-out, and when the server reports it spent or
expired (`TOO_MANY_ATTEMPTS_REQUEST_NEW_CODE`,
`INVALID_TWO_FACTOR_COOKIE`); a wrong code (`INVALID_CODE`,
`INVALID_BACKUP_CODE`) keeps it.
- **`LoginForm`.** While a challenge is held it draws the new internal
`TwoFactorChallengeForm` in place of its form: the code field
(`inputMode="numeric"`, `autocomplete="one-time-code"`, spaces
stripped), a link to a backup code and back, "Back to sign in", and the
error in place. `onSuccess` fires only once the code is verified.
Because the challenge lives in the provider, the console's dev-admin
chip leads to the same step (its spinner now stops there). Controls use
the phone floor #29136 introduced (`AUTH_TOUCH_TARGET_CLASS`, 44 px, 16
px input text).
- **Strings.** Ten `LoginFormLabels` members (`twoFactor*`), passed by
both login pages from `auth.login.twoFactor*`, and five refusal codes in
`signInRefusalMessages` from `auth.login.errors.twoFactor*`, in all ten
packs.
No server package is touched; no metadata key or option is added.
## Tests
- New `ui/packages/auth/src/__tests__/twoFactorSignIn-29105.test.tsx`
(real `AuthProvider` + `LoginForm` over a stub client): a TOTP user
signs in with password then code (`onSuccess` only after the code); a
wrong code shows the mapped error in place and a retry succeeds; a
backup code works; "Back to sign in" returns to the password step; a
dropped challenge returns there with the reason; no authenticator method
offers backup codes only; a user without two-factor is signed straight
in, unchanged.
- New
`ui/packages/auth/src/__tests__/twoFactorSignIn-client-29105.test.ts`:
the client maps `twoFactorRedirect` to a challenge and stores no token,
keeps the plain session path, posts each code kind to its endpoint, and
keeps the server `code` on a refusal.
- `createAuthClient.test.ts`: one assertion reads `result.user?.email`
for the widened `signIn` answer.
- Ablation, after committing: `LoginForm`'s challenge branch disabled
reds 6 of the 7 LoginForm cases (the unchanged non-two-factor case stays
green, as it should); the client's `twoFactorRedirect` check disabled
reds 2 of 6 client cases. `ablation-replace.mjs` restored each file
(empty `git diff HEAD`).
## Run record
Live, on `pnpm dev:crm -- --fresh` with `OS_AUTH_TWO_FACTOR=true`, the
seeded admin enrolled through `/two-factor/enable` with a TOTP computed
from the served secret.
| Step | Answer |
|---|---|
| password sign-in, enrolled | 200
`{"twoFactorRedirect":true,"twoFactorMethods":["totp"]}`, session
cookies expired, `two_factor` cookie set |
| verify-totp, wrong code | 401 `INVALID_CODE` |
| verify-totp, right code | 200 `{ token, user }`, session cookie set,
`two_factor` expired; `get-session` with the token answers the admin |
| verify-backup-code / wrong | 200 `{ token, user }` / 401
`INVALID_BACKUP_CODE` |
| verify-totp with no challenge cookie | 401 `INVALID_TWO_FACTOR_COOKIE`
|
Real browser (Playwright Chromium, the Console from `ui/apps/console`
source through Vite over that backend): at 1280 px the password step
leads to the code step with no token stored; a wrong code shows "That
code is not valid. Check your authenticator app and try again." in
place; the right code (typed as "123 456") lands on `/home`; "Back to
sign in" returns to the password step; a backup code signs in. At 375 x
812 (`isMobile`): the field, Verify and both links are 44 px tall, the
field text is 16 px, no horizontal scroll, and with the viewport shrunk
by a 336 px keyboard the field (263 to 307) and Verify (323 to 367) sit
inside the 476 px left. Screenshots are in the project folder
`issue-29105/`, not on GitHub, as the card asks.
## Acceptance notes
- `AuthClient` gains a required method, `verifySignInTwoFactor`, like
every earlier addition to that interface: a third-party `AuthClient`
implementation must add it to type-check. Declared `Clause-②: yes
(widening)` as claimed.
- The password step's own lockout code `ACCOUNT_LOCKED` has no entry in
`signInRefusalMessages` (before this PR too), so it shows the server's
English message; this PR adds only the two-factor step's codes.
- After verification the session is `{ token }`, as phone sign-in
already does: the verify endpoints answer `{ token, user }` with no
session envelope, and the provider's session reads only the token.
## Verification
Readings at head `606f6f61` (main `f2bf2938` merged, which carries
#29136 and #29159). Main `5780fa49` was then merged cleanly (head
`d35fdccc`); its only overlap is the ten locale packs, so on `d35fdccc`
the auth and i18n suites plus the every-member label pin were re-run
(139 files, 1821 tests passed) with the five PR-body and changeset gates
(all exit 0). CI's check-runs carry every family CI runs; they are not
re-run here.
- `node scripts/pm/dispatch-gates.mjs --ran`: 34 derived, 34 run, every
exit 0 (`check-changeset-no-major`, `check-adr-0087-registration` and
`check-closing-keyword-parity` read this body).
- `node scripts/check-sdui-manifest.mjs`: exit 0.
- `pnpm --dir ui exec vitest run scripts/`: 169 files passed, 2 skipped.
- `pnpm --dir ui check:new-line-citations`: 0 new citations.
<details><summary>Suites and type-check</summary>
- `pnpm --dir ui exec vitest run packages/auth/ packages/i18n/` plus the
33 console and app-shell test files that name the login pages or
`signInRefusalMessages` (the every-member label pin among them): Test
Files 171 passed, Tests 2013 passed, 13 skipped.
- `turbo run type-check --filter=@object-ui/auth
--filter=@object-ui/app-shell --filter=@object-ui/console
--filter=@object-ui/i18n`: 74 tasks successful.
- Dogfood battery: not owed, no `!` in the title and no `(narrowing)`
arm.
</details>
🤖 Generated with [Claude Code](https://claude.com/claude-code)
https://claude.ai/code/session_01QsR2jU7rCo15awmBUAqJ2C
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent 46e2400 commit d8b7ef4
26 files changed
Lines changed: 1041 additions & 14 deletions
File tree
- .changeset
- ui
- apps/console/src/pages/auth
- packages
- app-shell/src/console/auth
- auth/src
- __tests__
- i18n/src/locales
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
415 | 415 | | |
416 | 416 | | |
417 | 417 | | |
418 | | - | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
419 | 425 | | |
420 | 426 | | |
421 | 427 | | |
| |||
595 | 601 | | |
596 | 602 | | |
597 | 603 | | |
| 604 | + | |
| 605 | + | |
| 606 | + | |
| 607 | + | |
| 608 | + | |
| 609 | + | |
| 610 | + | |
| 611 | + | |
| 612 | + | |
| 613 | + | |
| 614 | + | |
| 615 | + | |
598 | 616 | | |
599 | 617 | | |
600 | 618 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
204 | 204 | | |
205 | 205 | | |
206 | 206 | | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
207 | 219 | | |
208 | 220 | | |
209 | 221 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
29 | 48 | | |
30 | 49 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
| 10 | + | |
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| |||
31 | 31 | | |
32 | 32 | | |
33 | 33 | | |
34 | | - | |
35 | | - | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
36 | 58 | | |
37 | 59 | | |
38 | 60 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
11 | | - | |
| 11 | + | |
| 12 | + | |
12 | 13 | | |
13 | 14 | | |
14 | 15 | | |
| |||
191 | 192 | | |
192 | 193 | | |
193 | 194 | | |
| 195 | + | |
| 196 | + | |
194 | 197 | | |
195 | 198 | | |
196 | 199 | | |
| |||
393 | 396 | | |
394 | 397 | | |
395 | 398 | | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
396 | 406 | | |
397 | 407 | | |
| 408 | + | |
398 | 409 | | |
399 | 410 | | |
400 | 411 | | |
| |||
406 | 417 | | |
407 | 418 | | |
408 | 419 | | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
409 | 447 | | |
410 | 448 | | |
411 | 449 | | |
| |||
445 | 483 | | |
446 | 484 | | |
447 | 485 | | |
| 486 | + | |
448 | 487 | | |
449 | 488 | | |
450 | 489 | | |
| |||
1108 | 1147 | | |
1109 | 1148 | | |
1110 | 1149 | | |
| 1150 | + | |
| 1151 | + | |
| 1152 | + | |
1111 | 1153 | | |
1112 | 1154 | | |
1113 | 1155 | | |
| |||
1155 | 1197 | | |
1156 | 1198 | | |
1157 | 1199 | | |
1158 | | - | |
| 1200 | + | |
1159 | 1201 | | |
1160 | 1202 | | |
1161 | 1203 | | |
| |||
0 commit comments