Repository navigation
Commit f35b361
fix(console): the sign-in page finishes the mobile sign-in hand-off instead of landing on a missing Console page (#29979)
<!-- claude-projects-attribution: {"github_login":"os-support-ai"} -->
_Requested by **Yibo Wang** · [project
thread](https://claude.ai/code/project/chan_01BVUyyPie8gHwoPM18iABwB?thread=cmsg_01BVUyyPie8gHwoPM18iABwB9ZFKq8kvUEM1zfaJkpDrs7)_
Fixes #29971
## 维护者速读
- **改了什么**:Console 的登录页(和注册页)在 `?redirect=` 恰好指向移动端 hand-off
路由时,登录后直接用脚本跳过去(不再拼进 `/_console`),并且排在选组织之前;浏览器里已有的会话若超过 5
分钟,先显示登录表单,重新登录后再跳。
- **为什么改**:移动端在系统浏览器里打开登录页完成 Google 等登录后,本该跳到 hand-off 拿 302 回
App,现在却落在「页面不存在」,服务端在 18.3.0 已上线的那一半没人能用到。
- **风险与回滚**:只认 hand-off 这一条路径(同源、同 auth base、路径完全相等),其他所有 redirect
行为不变;回滚即 revert 本 PR,登录页回到旧行为。
-
**先看**:`ui/apps/console/src/pages/auth/handoffRedirect.ts`(`handoffNavigationTarget`
的匹配条件、`useHandoffRedirect` 的五分钟判定),`LoginPage.tsx` 登录后编排里新增的第 2 步。
- **两分钟验证**:`pnpm --dir ui exec vitest run
apps/console/src/pages/auth/__tests__/signInHandoff-29971.test.tsx
apps/console/src/pages/auth/__tests__/handoffRedirect-29971.test.ts`,预期
2 个文件、29 条全部通过。
- **需要你做的**:这是会话转移(auth)流程,请维护者审阅并合并本 PR。
Clause-②: no
Metadata-protocol: none
## Design: the five-minute session
**What the auth client exposes (measured).**
`createAuthClient().getSession()` hands back the `session` object of
better-auth's `get-session` answer as received, so the server's
`createdAt` is there at runtime even though `AuthClientSession` does not
declare it: a `Date` through better-auth's client JSON parser (it
revives ISO dates), an ISO string through the raw-`fetch` stale-bearer
retry. A sign-in answer is different: `/sign-in/email` answers `{
redirect, token, url, user }`, so after a password sign-in the provider
holds `session: undefined` (two-factor and phone sign-ins hold `{ token
}`). No new endpoint and no change to `@object-ui/auth` was needed.
**The choice: re-authenticate on top of the old session, not sign it out
first.** On a hand-off redirect, a session older than five minutes keeps
the ordinary sign-in form on screen; signing in on it creates a new
session, the sign-in answer sets this browser's session cookie to it,
and the page forwards. Why not sign the old session out first:
1. `signOut()` in the auth client navigates to the provider's
end-session URL whenever the server answers one (RP-initiated logout for
a federated session). For a visitor whose stale session came from SSO,
that takes the browser off the sign-in page and ends the hand-off
attempt.
2. On Android, Custom Tabs share Chrome's cookies, which is exactly how
a stale session arrives. Signing it out also signs the user out of the
web Console in Chrome; signing in on top only replaces this browser's
cookie.
3. It adds a request with its own failure leg (a sign-out that fails
leaves the state unknown) and buys nothing: the hand-off route is the
authority on freshness and refuses an old session itself.
The cost, stated: the old session row is not revoked; it stays valid
until it expires, with no cookie in this browser pointing at it. A
visitor may sign in as a different account on that form; the hand-off
then carries the account they signed in as.
**How "older than five minutes" is decided.** Each session the page
holds is judged once, when it arrives, in a layout effect (the clock is
never read during render): with a `createdAt`, its age decides
(`HANDOFF_SESSION_MAX_AGE_MS`, restating the route's
`ONE_TIME_TOKEN_HANDOFF_SESSION_MAX_AGE_SECONDS`); without one, the
session is fresh only if it is not the one the first session check
answered, which means a sign-in on this page made it. A session found on
arrival with no `createdAt`, or one not judged yet, holds the form. A
wrong verdict on the page is never a security question (the route
refuses on its own clock); it only decides between showing the form and
forwarding to the route's own `401`.
## What changes
- `ui/apps/console/src/pages/auth/handoffRedirect.ts` (new):
`handoffNavigationTarget` answers the navigation target for a redirect
that names the hand-off, or `null`. The route is built with the spec's
`getAuthEndpointUrl(base, 'oneTimeTokenHandoff')` over the auth base
every Console auth page uses (`VITE_SERVER_URL` + `/api/v1/auth`, the
URL `App.tsx` hands `AuthProvider`). A match needs a root-relative
redirect on this page's origin whose parsed path equals the route's path
exactly; the answer is re-serialized from the parsed URL.
`useHandoffRedirect` adds the five-minute verdict above.
- `LoginPage.tsx`: the post-sign-in orchestration gains a step 2, after
the OAuth `/oauth2/authorize` replay and before the organization steps:
a hand-off target is navigated to once with `window.location.assign`,
unprefixed; a session the route would refuse returns without navigating,
and the page renders its form for it (the bootstrap probe for the
sign-up offer runs for that visitor as for a signed-out one). Every
other redirect keeps `withConsoleBase`.
- `RegisterPage.tsx`: the same step in its post-sign-up orchestration
(rules 1 to 4), so a user who follows "Sign up" from the system auth
session ends on the hand-off too.
- One `@object-ui/console` patch changeset.
Rule 2 needs no code: the social buttons' default `callbackURL` is the
current page, and neither page passes the hand-off as one; a pin holds
that.
## Why
The mobile shell (objectstack-ai/mobile#148) opens the Console's sign-in
page in the system auth session because some providers refuse embedded
WebViews. The server half (#29766) answers the hand-off with `302
SCHEME://…?token=T`, but the sign-in page treated the redirect as a
Console route and the browser landed on a Console page that does not
exist.
## Tests
<details><summary>New pins (2 files, 29 tests)</summary>
`pages/auth/__tests__/handoffRedirect-29971.test.ts` (the matcher and
the verdict):
- the route the spec names under the auth base; the hand-off path with
its query answers itself, unprefixed;
- CONTROL `/api/v1/data/x`, `/settings`, `/` answer `null`;
- lookalikes answer `null`: `…/handoffX`, a trailing slash, an extra
segment, `…/verify`, upper case, an encoded slash, the path under
`/_console`;
- a different base path (`/tenant-a/api/v1/auth`) both ways;
- absolute, protocol-relative, backslash-smuggled and tab-smuggled URLs,
and `javascript:`;
- an auth base on another origin;
- the five-minute window (4 min, exactly 5, 6 min, a week), a `Date` or
an ISO string, a session without `createdAt` found on arrival vs made by
a sign-in, an unreadable `createdAt`.
`pages/auth/__tests__/signInHandoff-29971.test.tsx` (the real
`AuthProvider` over a mock client, the Console mounted as the shipped
embeddable build, `base href /_console/`):
- rule 1: the hand-off is assigned unprefixed and lands on the route's
path; CONTROL `/api/v1/data/x` lands on `/_console/api/v1/data/x`; a
lookalike lands under `/_console`; an absolute `https://evil.example…`
lands on `/_console/`;
- rule 2: a provider sign-in's `callbackURL` and `errorCallbackURL` are
this sign-in page, carrying the hand-off in `?redirect=`, and nothing
navigates;
- rule 3: several organizations with none active go to the hand-off, not
`/organizations`; a single organization whose activation never answers
still goes to the hand-off;
- rule 4: a 6-minute-old session shows the form and does not navigate; a
sign-in on that form forwards; a session found without `createdAt` is
held; a visitor who arrives signed out is forwarded once signed in;
CONTROL: an hour-old session on `?redirect=/settings` still forwards as
today;
- RegisterPage: a fresh session goes to the hand-off; CONTROL
`/api/v1/data/x`; a 6-minute-old session shows the sign-up form and a
sign-up on it forwards.
</details>
<details><summary>Ablation (the hand-off branch dropped)</summary>
Run under `scripts/pm/os-verify-lock.sh` with
`scripts/ablation-replace.mjs` on `LoginPage.tsx`: the anchor ` if
(handoff.target) {` replaced by ` if (false && handoff.target) {`
(anchor hits 1 → 0, blob `5339378f1236` → `98b3e8304e15`), then `pnpm
--dir ui exec vitest run
apps/console/src/pages/auth/__tests__/signInHandoff-29971.test.tsx -t
LoginPage`:
- `Tests 7 failed | 5 passed | 3 skipped (15)` (the 3 skipped are the
RegisterPage pins, outside `-t LoginPage`).
- Red: rule 1 "navigates to the hand-off unprefixed, outside the Console
mount" (`assign` called twice, not once), both rule-3 pins (`expected
'./api/v1/auth/one-time-token/handoff?…' to be
'/api/v1/auth/one-time-token/handoff?r…'`: the old `withConsoleBase`
target, which lands under `/_console/`), and four rule-4 pins.
- Green under the ablation, as they should be: the three rule-1
CONTROL/lookalike/absolute pins, rule 2, and the rule-4 CONTROL on
`?redirect=/settings`.
- Restored: blob back to `5339378f1236` (= HEAD), `git diff HEAD` empty.
</details>
## Acceptance notes
- **RegisterPage: handled.** The same step, rules 1 to 4, in its
post-sign-up orchestration. Not changed: its `verificationCallbackURL`.
When a deployment requires email verification, the verification mail's
link still resolves `?redirect=` as a Console route. That link opens
from the mail app, outside the system auth session the mobile shell is
waiting on, so a hand-off from there could not return to the app anyway.
Whether the shell wants a sign-up that needs verification to come back
some other way is the mobile card's call, not this page's.
- **`os serve` cannot register an app scheme (out of scope, server/cli
lane).** `packages/cli` composes `AuthPlugin` without
`trustedAppSchemes`, and neither a stack's config nor a settings key or
env var reaches it (0 hits for `trustedAppSchemes` under `packages/cli`
and `packages/runtime`). Measured on a stock `pnpm dev:crm` boot: a
signed-in, same-origin `GET
/api/v1/auth/one-time-token/handoff?redirect_uri=ai.objectos.mobile://auth/callback…`
answers `400 VALIDATION_ERROR`, with the message: The "redirect_uri"
scheme "ai.objectos.mobile" is not a registered app scheme, so no token
is handed to it. A deployment registers the app schemes it hands
sign-ins to in its auth configuration. That boot has no auth
configuration that could register one. The live check below therefore
used a local, measurement-only one-line patch to
`packages/cli/src/commands/serve.ts`; it was never committed, and the
source and the cli dist were restored afterwards.
- **Drift risk.** `HANDOFF_SESSION_MAX_AGE_MS` restates plugin-auth's
`ONE_TIME_TOKEN_HANDOFF_SESSION_MAX_AGE_SECONDS`, because that module is
server-only and this card does not touch
`packages/plugins/plugin-auth/**` or `packages/spec/**`. If the server
window changes, the page's copy decides only whether a visitor sees the
form or the route's own `401`; the route stays the authority.
- **Fails closed.** With a `VITE_SERVER_URL` on another origin, the
matcher answers `null` and the redirect keeps today's Console-route
behaviour. A skewed client clock moves only the verdict on a session
found on arrival (form vs the route's `401`); a session made by a
sign-in on the page carries no `createdAt` and is judged as made here.
- **Lint.** eslint on the touched files: 0 errors, 5 warnings. Four are
the existing `react-hooks/set-state-in-effect` warnings in the two
pages; one is new, the same rule (warn level), on the layout effect that
records the verdict in `handoffRedirect.ts`.
- **Type-check coverage of the new test files: not shown.** The Console
`type-check` exited 0 and its tsconfig includes `src`, but a
`--listFiles` count for the two new test files read 0, so I do not claim
they are type-checked by that lane (vitest compiles them without
type-checking).
## Verification
All on head `c6ac92e1`; the builds and test runs under
`scripts/pm/os-verify-lock.sh`, their exit codes read from its `VERDICT
command-exit` line:
| Check | Result |
| --- | --- |
| `pnpm --filter '@object-ui/console^...' build` (the closure) | exit 0
(9m32s) |
| `pnpm --dir ui exec vitest run` on the two new files | 2 files, 29 /
29 passed |
| `pnpm --filter @object-ui/console run build` then `run type-check` |
exit 0 (2m55s) |
| `node ui/scripts/check-eager-closure-budget.mjs` on that build |
2109.4 KB gzipped over 104 of 2401 chunks, budget 2431.6 KB (headroom
322.2 KB); `vendor-objectstack`, where the new module lands, 577.4 /
823.0 KB. No `main` baseline measured. |
| `pnpm --dir ui exec vitest run --project @object-ui/console` | 295 /
295 files, 2870 / 2870 tests, exit 0 (20m43s) |
| eslint on the touched files | 0 errors, 5 warnings (see Acceptance
notes) |
| `node scripts/check-sdui-manifest.mjs` (+ `--self-test`) | exit 0,
exit 0 |
| `node scripts/pm/dispatch-gates.mjs --commands` | 34 commands, every
one exit 0 |
| `node scripts/pm/dispatch-gates.mjs --ran` | `34 derived famil(ies)
accounted for — 34 run, 0 NOT-MEASURED` |
**Live, in a browser** (Playwright Chromium against `pnpm dev:crm --
--fresh`, the Console built from this head and served at `/_console/`,
the dev-seeded admin; the CLI carried the measurement-only
`trustedAppSchemes: ['ai.objectos.mobile']` noted above):
- **Fresh sign-in.** `/_console/login?redirect=` naming the hand-off,
then a password sign-in on the form: the browser's next navigation is
`GET
/api/v1/auth/one-time-token/handoff?redirect_uri=ai.objectos.mobile%3A%2F%2Fauth%2Fcallback&code_challenge=…&code_challenge_method=S256`
with `Sec-Fetch-Site: same-origin` and `Sec-Fetch-Mode: navigate`,
answered `302` with `Location:
ai.objectos.mobile://auth/callback?token=` and a token (redacted in the
log). No "page not found". Headless Chromium has no handler for the app
scheme, so the tab stays put after the `302`; on a device the system
auth session hands that URL to the app.
- **CONTROL.** The same sign-in on `?redirect=/api/v1/data/x` navigates
to `/_console/api/v1/data/x`, a Console route, as before.
- **A session older than five minutes.** A browser signed in at
21:39:21Z (`get-session` `createdAt` read back), opened on the hand-off
redirect at 21:45:00Z: the sign-in form is shown and no hand-off request
is made. The route itself answers that session's cookie with `401`.
Signing in on the form then ends in the same `302` with a token,
`Sec-Fetch-Site: same-origin`. The old session still answers
`get-session` afterwards, as the design above says (not revoked).
- **Provider sign-in: NOT MEASURED live.** The container has no identity
provider (`/api/v1/auth/config` lists no social providers). Rule 2 is
held by the unit pin above.
## Human Minimum
- [x] 1. Schemas start as Zod; `packages/spec` holds no runtime logic.
(No `packages/spec` change.)
- [x] 2. Every name I added uses the casing for its kind.
- [x] 3. No formatter ran; eslint on the touched files reports 0 errors
(CI's Lint UI is the authority).
- [x] 4. One issue, one branch off an up-to-date `main`, one pull
request.
- [x] 5. A change that publishes carries a changeset, and both
declaration lines are filled in here and in the changeset.
- [x] 6. `packages/spec` touched: not touched.
- [x] 7. No hand edits to `content/docs/references/`,
`packages/*/CHANGELOG.md` or `content/docs/releases/`.
- [x] 8. Title, body and comments are in English (the maintainer brief
above is Chinese by the project's convention).
- [x] The *Before Submitting* list in CONTRIBUTING.md is done.
---
_Generated by [Claude
Code](https://claude.ai/code/session_01WGufzfukSC9MVr7odALmfg)_
---------
Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: objectstack-fleet[bot] <332303061+objectstack-fleet[bot]@users.noreply.github.com>1 parent 89803c5 commit f35b361
6 files changed
Lines changed: 634 additions & 7 deletions
File tree
- .changeset
- ui/apps/console/src/pages/auth
- __tests__
| 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 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
13 | | - | |
14 | | - | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
15 | 16 | | |
16 | 17 | | |
17 | 18 | | |
| |||
73 | 74 | | |
74 | 75 | | |
75 | 76 | | |
| 77 | + | |
76 | 78 | | |
77 | 79 | | |
78 | 80 | | |
| |||
103 | 105 | | |
104 | 106 | | |
105 | 107 | | |
| 108 | + | |
106 | 109 | | |
107 | 110 | | |
108 | 111 | | |
109 | 112 | | |
110 | 113 | | |
111 | 114 | | |
112 | 115 | | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
113 | 123 | | |
114 | 124 | | |
115 | 125 | | |
| |||
141 | 151 | | |
142 | 152 | | |
143 | 153 | | |
| 154 | + | |
| 155 | + | |
144 | 156 | | |
145 | 157 | | |
146 | 158 | | |
| |||
159 | 171 | | |
160 | 172 | | |
161 | 173 | | |
162 | | - | |
| 174 | + | |
163 | 175 | | |
164 | 176 | | |
165 | 177 | | |
| |||
249 | 261 | | |
250 | 262 | | |
251 | 263 | | |
252 | | - | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
253 | 279 | | |
254 | 280 | | |
255 | 281 | | |
| |||
282 | 308 | | |
283 | 309 | | |
284 | 310 | | |
| 311 | + | |
| 312 | + | |
285 | 313 | | |
286 | 314 | | |
287 | | - | |
| 315 | + | |
288 | 316 | | |
289 | 317 | | |
290 | 318 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
| 26 | + | |
| 27 | + | |
26 | 28 | | |
27 | 29 | | |
28 | 30 | | |
| |||
52 | 54 | | |
53 | 55 | | |
54 | 56 | | |
| 57 | + | |
55 | 58 | | |
56 | 59 | | |
57 | 60 | | |
| |||
77 | 80 | | |
78 | 81 | | |
79 | 82 | | |
| 83 | + | |
80 | 84 | | |
81 | 85 | | |
82 | 86 | | |
83 | 87 | | |
84 | 88 | | |
85 | 89 | | |
86 | 90 | | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
87 | 96 | | |
88 | 97 | | |
89 | 98 | | |
| |||
105 | 114 | | |
106 | 115 | | |
107 | 116 | | |
| 117 | + | |
| 118 | + | |
108 | 119 | | |
109 | 120 | | |
110 | 121 | | |
| |||
143 | 154 | | |
144 | 155 | | |
145 | 156 | | |
146 | | - | |
| 157 | + | |
147 | 158 | | |
148 | 159 | | |
149 | 160 | | |
| |||
176 | 187 | | |
177 | 188 | | |
178 | 189 | | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
179 | 201 | | |
180 | 202 | | |
181 | 203 | | |
| |||
203 | 225 | | |
204 | 226 | | |
205 | 227 | | |
| 228 | + | |
| 229 | + | |
206 | 230 | | |
207 | 231 | | |
208 | 232 | | |
209 | 233 | | |
210 | | - | |
| 234 | + | |
211 | 235 | | |
212 | 236 | | |
213 | 237 | | |
| |||
| 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 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
0 commit comments