cursedauth 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (108) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +87 -0
  3. package/dist/account/AccountMenu.d.ts +153 -0
  4. package/dist/account/AccountMenu.d.ts.map +1 -0
  5. package/dist/account/AccountMenu.js +388 -0
  6. package/dist/account/AccountMenu.js.map +1 -0
  7. package/dist/account/AccountNotice.d.ts +35 -0
  8. package/dist/account/AccountNotice.d.ts.map +1 -0
  9. package/dist/account/AccountNotice.js +133 -0
  10. package/dist/account/AccountNotice.js.map +1 -0
  11. package/dist/account/accountNotice.state.d.ts +20 -0
  12. package/dist/account/accountNotice.state.d.ts.map +1 -0
  13. package/dist/account/accountNotice.state.js +55 -0
  14. package/dist/account/accountNotice.state.js.map +1 -0
  15. package/dist/account/avatar.d.ts +37 -0
  16. package/dist/account/avatar.d.ts.map +1 -0
  17. package/dist/account/avatar.js +47 -0
  18. package/dist/account/avatar.js.map +1 -0
  19. package/dist/accountSlot.client.d.ts +113 -0
  20. package/dist/accountSlot.client.d.ts.map +1 -0
  21. package/dist/accountSlot.client.js +330 -0
  22. package/dist/accountSlot.client.js.map +1 -0
  23. package/dist/accountSlots.d.ts +192 -0
  24. package/dist/accountSlots.d.ts.map +1 -0
  25. package/dist/accountSlots.js +274 -0
  26. package/dist/accountSlots.js.map +1 -0
  27. package/dist/auth.d.ts +167 -0
  28. package/dist/auth.d.ts.map +1 -0
  29. package/dist/auth.js +310 -0
  30. package/dist/auth.js.map +1 -0
  31. package/dist/auth.server.d.ts +35 -0
  32. package/dist/auth.server.d.ts.map +1 -0
  33. package/dist/auth.server.js +67 -0
  34. package/dist/auth.server.js.map +1 -0
  35. package/dist/clientDescription.d.ts +186 -0
  36. package/dist/clientDescription.d.ts.map +1 -0
  37. package/dist/clientDescription.js +278 -0
  38. package/dist/clientDescription.js.map +1 -0
  39. package/dist/constantTime.d.ts +35 -0
  40. package/dist/constantTime.d.ts.map +1 -0
  41. package/dist/constantTime.js +46 -0
  42. package/dist/constantTime.js.map +1 -0
  43. package/dist/contentCensus.d.ts +59 -0
  44. package/dist/contentCensus.d.ts.map +1 -0
  45. package/dist/contentCensus.js +55 -0
  46. package/dist/contentCensus.js.map +1 -0
  47. package/dist/index.d.ts +25 -0
  48. package/dist/index.d.ts.map +1 -0
  49. package/dist/index.js +24 -0
  50. package/dist/index.js.map +1 -0
  51. package/dist/mountGatedApp.d.ts +128 -0
  52. package/dist/mountGatedApp.d.ts.map +1 -0
  53. package/dist/mountGatedApp.js +107 -0
  54. package/dist/mountGatedApp.js.map +1 -0
  55. package/dist/runtimeMode.d.ts +28 -0
  56. package/dist/runtimeMode.d.ts.map +1 -0
  57. package/dist/runtimeMode.js +37 -0
  58. package/dist/runtimeMode.js.map +1 -0
  59. package/dist/secureRequest.d.ts +22 -0
  60. package/dist/secureRequest.d.ts.map +1 -0
  61. package/dist/secureRequest.js +27 -0
  62. package/dist/secureRequest.js.map +1 -0
  63. package/dist/sso.d.ts +373 -0
  64. package/dist/sso.d.ts.map +1 -0
  65. package/dist/sso.js +420 -0
  66. package/dist/sso.js.map +1 -0
  67. package/dist/ssoAutoSignIn.d.ts +272 -0
  68. package/dist/ssoAutoSignIn.d.ts.map +1 -0
  69. package/dist/ssoAutoSignIn.js +431 -0
  70. package/dist/ssoAutoSignIn.js.map +1 -0
  71. package/dist/ssoConsumer.server.d.ts +295 -0
  72. package/dist/ssoConsumer.server.d.ts.map +1 -0
  73. package/dist/ssoConsumer.server.js +972 -0
  74. package/dist/ssoConsumer.server.js.map +1 -0
  75. package/dist/stageIdentity.d.ts +72 -0
  76. package/dist/stageIdentity.d.ts.map +1 -0
  77. package/dist/stageIdentity.js +74 -0
  78. package/dist/stageIdentity.js.map +1 -0
  79. package/dist/ui/popover.d.ts +190 -0
  80. package/dist/ui/popover.d.ts.map +1 -0
  81. package/dist/ui/popover.js +351 -0
  82. package/dist/ui/popover.js.map +1 -0
  83. package/dist/userSessions.d.ts +205 -0
  84. package/dist/userSessions.d.ts.map +1 -0
  85. package/dist/userSessions.js +419 -0
  86. package/dist/userSessions.js.map +1 -0
  87. package/package.json +115 -0
  88. package/src/account/AccountMenu.tsx +729 -0
  89. package/src/account/AccountNotice.tsx +223 -0
  90. package/src/account/accountNotice.state.ts +66 -0
  91. package/src/account/avatar.ts +50 -0
  92. package/src/accountSlot.client.ts +362 -0
  93. package/src/accountSlots.ts +305 -0
  94. package/src/auth.server.ts +89 -0
  95. package/src/auth.ts +515 -0
  96. package/src/clientDescription.ts +366 -0
  97. package/src/constantTime.ts +45 -0
  98. package/src/contentCensus.ts +106 -0
  99. package/src/index.ts +58 -0
  100. package/src/mountGatedApp.ts +240 -0
  101. package/src/runtimeMode.ts +37 -0
  102. package/src/secureRequest.ts +25 -0
  103. package/src/sso.ts +767 -0
  104. package/src/ssoAutoSignIn.ts +544 -0
  105. package/src/ssoConsumer.server.ts +1260 -0
  106. package/src/stageIdentity.ts +76 -0
  107. package/src/ui/popover.ts +390 -0
  108. package/src/userSessions.ts +678 -0
@@ -0,0 +1,431 @@
1
+ /**
2
+ * Automatic SSO sign-in — the private satellites stop asking for a click they
3
+ * already know the answer to (2026-08-01, owner ask).
4
+ *
5
+ * ── What it changes ─────────────────────────────────────────────────────────
6
+ * Every private satellite (roms, hobbies, notes, vault, orch, orch-companion,
7
+ * station) drew the same screen when signed out: one sentence and one "Sign in
8
+ * with your cursedalchemy account" button, whose only effect is a navigation to
9
+ * `/api/auth/sso/start`. For someone who already holds an auth session — the
10
+ * normal case, since the whole fleet shares ONE user set — that screen is a
11
+ * speed bump in front of a redirect that was always going to succeed. The
12
+ * button now presses itself; the screen is what you see when it CAN'T.
13
+ *
14
+ * ── Why the guards are the whole file ───────────────────────────────────────
15
+ * An unconditional "anonymous → navigate to auth" is a redirect LOOP generator,
16
+ * and the loops it makes are the invisible kind: the browser bounces between
17
+ * two origins with no error anywhere, forever. Four situations must suppress
18
+ * it, and each is a real failure that has already happened somewhere in this
19
+ * fleet:
20
+ *
21
+ * 1. **This page IS the failure landing.** The consumer's callback redirects
22
+ * to `/?ssoError=…` (expired grant, replay, a non-admin account at an
23
+ * admin-only app). Auto-starting there would send the person straight back
24
+ * to auth, which would hand out another grant the app refuses for the same
25
+ * reason, forever — and they would never read why.
26
+ * 2. **The session did not stick.** The round trip appears to succeed and the
27
+ * probe still says anonymous — a rejected cookie (SameSite, a clock, a
28
+ * misconfigured `publicUrl`). Nothing in the URL says so, which is exactly
29
+ * why the ATTEMPT is remembered: a second anonymous load inside the
30
+ * cooldown means the loop is already running, and we stop and show the
31
+ * button instead.
32
+ * 3. **They just signed out.** Auth's own session outlives this app's cookie,
33
+ * so an auto-start after "Sign out" signs you straight back in. The button
34
+ * is then the only way to leave. `markSsoSignedOut()` is the flag, and it
35
+ * lasts for the tab.
36
+ * 4. **This door is not an SSO door.** The local vault daemon
37
+ * (`createLocalOwnerAuth`) answers `/api/auth/session` with
38
+ * `accountsUrl: ""` on purpose, and a dev open loop answers 404. Sending
39
+ * either to `auth.cursedalchemy.com` — on the box that sleeps 12am–10am CT
40
+ * — would break the instances whose whole point is working offline. So the
41
+ * trigger is a NON-EMPTY `accountsUrl`: the server says where its door is,
42
+ * and no client has to guess.
43
+ *
44
+ * The state lives in `sessionStorage`: per tab, gone when the tab closes, and
45
+ * never a reason to refuse a sign-in tomorrow. Every access is wrapped —
46
+ * Safari's private mode throws on `sessionStorage` access, and an app that
47
+ * cannot store a guard should still be openable (it degrades to the button,
48
+ * never to a loop).
49
+ */
50
+ /** Where the consumer's redirect chain begins. Kept here rather than imported
51
+ * from `ssoConsumer.server.ts`: that module pulls in `bun:sqlite` and hono,
52
+ * and this one runs in a browser. `ssoAutoSignIn.test.ts` pins them equal. */
53
+ import { useEffect, useRef, useState } from "react";
54
+ import { currentAccountSlot } from "./accountSlot.client";
55
+ import { ACCOUNT_SLOT_PARAM } from "./accountSlots";
56
+ export const SSO_START_PATH = "/api/auth/sso/start";
57
+ /**
58
+ * The GLOBAL sign-out door (2026-08-10). Ending this app's cookie was never a
59
+ * sign-out — auth's own session outlives it, so the next press of "Sign in"
60
+ * hands the browser straight back to the SAME person. That is not a cosmetic
61
+ * gap: it is why handing the laptop to somebody else did not work, because
62
+ * there was no way for them to become the signed-in user. This path revokes
63
+ * this app's session and then bounces through auth to revoke ITS session too,
64
+ * so "signed out" means what it says on every satellite at once.
65
+ */
66
+ export const SSO_SIGNOUT_PATH = "/api/auth/sso/signout";
67
+ /** When the last automatic attempt was launched, ms epoch, as a string. */
68
+ export const SSO_ATTEMPT_KEY = "satellite.sso.autoAttempt.v1";
69
+ /** Present when the person signed out in this tab. Value is unused. */
70
+ export const SSO_SIGNED_OUT_KEY = "satellite.sso.signedOut.v1";
71
+ /**
72
+ * How long one failed attempt suppresses the next.
73
+ *
74
+ * It is a ceiling on the round trip, not a guess at it: the hop is two 302s
75
+ * and a page load, so anything still anonymous 30 seconds later did not fail
76
+ * slowly, it failed silently. Long enough that a slow phone on 3G is never
77
+ * caught, short enough that the person who fixes the cause (signs in at auth
78
+ * in another tab, gets the admin flag) is not left staring at a button.
79
+ */
80
+ export const SSO_ATTEMPT_COOLDOWN_MS = 30_000;
81
+ /**
82
+ * The whole policy, as a pure function — so the guards are testable without a
83
+ * browser, a redirect, or the ability to observe a loop only by hanging.
84
+ */
85
+ export function decideSsoAutoSignIn(input) {
86
+ if (input.enabled === false)
87
+ return { start: false, reason: "disabled" };
88
+ if (input.authenticated)
89
+ return { start: false, reason: "authenticated" };
90
+ // The server names its own door. No accountsUrl → nothing to redirect to.
91
+ if (!input.accountsUrl)
92
+ return { start: false, reason: "not-an-sso-door" };
93
+ // Guard 1 — this load IS the failure. Never bounce off an explanation.
94
+ if (input.ssoError)
95
+ return { start: false, reason: "sign-in-failed" };
96
+ // Guard 3 — leaving must be possible.
97
+ if (input.signedOut)
98
+ return { start: false, reason: "signed-out" };
99
+ // Guard 2 — a round trip that came back anonymous is a loop in progress.
100
+ const cooldown = input.cooldownMs ?? SSO_ATTEMPT_COOLDOWN_MS;
101
+ if (input.lastAttemptAt !== null && input.now - input.lastAttemptAt < cooldown) {
102
+ return { start: false, reason: "recent-attempt" };
103
+ }
104
+ return { start: true };
105
+ }
106
+ // ── The browser side of it ──────────────────────────────────────────────────
107
+ const store = () => {
108
+ try {
109
+ return globalThis.sessionStorage ?? null;
110
+ }
111
+ catch {
112
+ // Safari private mode throws on ACCESS, not just on write.
113
+ return null;
114
+ }
115
+ };
116
+ const read = (key) => {
117
+ try {
118
+ return store()?.getItem(key) ?? null;
119
+ }
120
+ catch {
121
+ return null;
122
+ }
123
+ };
124
+ const write = (key, value) => {
125
+ try {
126
+ store()?.setItem(key, value);
127
+ }
128
+ catch {
129
+ /* a tab that cannot remember degrades to the button, never to a loop */
130
+ }
131
+ };
132
+ const drop = (key) => {
133
+ try {
134
+ store()?.removeItem(key);
135
+ }
136
+ catch {
137
+ /* ignore */
138
+ }
139
+ };
140
+ /** The last automatic attempt's timestamp, or null if there wasn't one (or the
141
+ * stored value is not a number — a hand-edited or half-written key must read
142
+ * as "no attempt", which fails toward trying rather than toward stuck). */
143
+ export function lastSsoAttemptAt() {
144
+ const raw = read(SSO_ATTEMPT_KEY);
145
+ if (raw === null)
146
+ return null;
147
+ const at = Number(raw);
148
+ return Number.isFinite(at) ? at : null;
149
+ }
150
+ export function ssoSignedOutHere() {
151
+ return read(SSO_SIGNED_OUT_KEY) !== null;
152
+ }
153
+ /**
154
+ * Call from every sign-out path. Auth's session outlives this app's cookie, so
155
+ * without this the next render signs the person straight back in and "Sign
156
+ * out" is a button that does nothing visible.
157
+ */
158
+ export function markSsoSignedOut() {
159
+ write(SSO_SIGNED_OUT_KEY, "1");
160
+ drop(SSO_ATTEMPT_KEY);
161
+ }
162
+ /** Clear both markers — what a confirmed session means. */
163
+ export function clearSsoAutoState() {
164
+ drop(SSO_ATTEMPT_KEY);
165
+ drop(SSO_SIGNED_OUT_KEY);
166
+ }
167
+ /** `?ssoError=` on the current URL, defensively (a malformed search string
168
+ * must not take the app down on its first render). */
169
+ export function ssoErrorFromLocation(search) {
170
+ try {
171
+ return new URLSearchParams(search ?? globalThis.location?.search ?? "").get("ssoError");
172
+ }
173
+ catch {
174
+ return null;
175
+ }
176
+ }
177
+ /**
178
+ * The `?ssoError=` this page load arrived with, read ONCE and remembered for as long as
179
+ * the page lives.
180
+ *
181
+ * ── 🔴 Why a cache, and why it is load-bearing (2026-08-25) ─────────────────
182
+ * `takeSsoError()` STRIPS the parameter, and two different things need to read it: the
183
+ * redirect guard in {@link decideSsoAutoSignIn} (guard 1 — "this page IS the failure
184
+ * landing, never bounce off an explanation") and whatever renders the message. Whichever
185
+ * runs first used to win, and the loser saw a URL with nothing on it.
186
+ *
187
+ * That is not symmetrical. If the NOTICE loses, a refusal is silent — bad. If the GUARD
188
+ * loses, the app immediately hops back to auth, which hands out another grant it refuses
189
+ * for the same reason, forever: the invisible redirect loop this whole module exists to
190
+ * prevent. React runs child effects before parent effects, so mounting a notice inside the
191
+ * signed-out branch of an app whose guard lives one component up loses the guard EVERY
192
+ * time — measured in a browser against collections' built artifact on 2026-08-25, where a
193
+ * `/?ssoError=…` load bounced straight to `auth.cursedalchemy.com`.
194
+ *
195
+ * Reading through here makes the order stop mattering. Both callers see the same answer
196
+ * whether or not the URL still carries it, so the hazard is not something a future reader
197
+ * has to remember — it is not expressible.
198
+ */
199
+ let capturedSsoError;
200
+ export function ssoErrorForThisLoad() {
201
+ if (capturedSsoError === undefined)
202
+ capturedSsoError = ssoErrorFromLocation();
203
+ return capturedSsoError;
204
+ }
205
+ /** Forget the captured value. A page load is the real scope, so ONLY a test needs this —
206
+ * a suite that exercises several landings inside one process. */
207
+ export function resetSsoErrorCapture() {
208
+ capturedSsoError = undefined;
209
+ }
210
+ /**
211
+ * Take `?ssoError=` off the URL, once, without touching anything else on it.
212
+ *
213
+ * 🔴 `replaceState`, never a reload or a hash change: this runs on a page that is
214
+ * already rendered and may be several routes deep, and either alternative would
215
+ * throw that away. Returns the message it removed (or null), so the ONE caller
216
+ * that consumed it is the one that reports it — a helper that both cleaned the
217
+ * URL and swallowed the message would make a failed sign-in silent, which is the
218
+ * exact bug this exists to fix.
219
+ */
220
+ export function takeSsoError() {
221
+ // 🔴 Through the cache, not off the URL. This function is what EMPTIES the URL, so a
222
+ // second caller — the redirect guard above all — must still be able to learn the answer
223
+ // afterwards. See `ssoErrorForThisLoad`.
224
+ const message = ssoErrorForThisLoad();
225
+ if (!message)
226
+ return null;
227
+ try {
228
+ const url = new URL(globalThis.location.href);
229
+ url.searchParams.delete("ssoError");
230
+ globalThis.history.replaceState(globalThis.history.state, "", `${url.pathname}${url.search}${url.hash}`);
231
+ }
232
+ catch {
233
+ /* a browser that cannot rewrite the URL still gets the message */
234
+ }
235
+ return message;
236
+ }
237
+ /**
238
+ * Report a refused sign-in ONCE, then take it off the URL (2026-08-15).
239
+ *
240
+ * The owner, switching accounts from `family`: *"I am auto directed back to
241
+ * family site as the user I was already. I see no indication that it did not
242
+ * work except the URL showing a query string with an ssoError field … there
243
+ * should be an indicator such as a toast popup tells me the action failed in a
244
+ * user friendly way so I quickly know what went wrong, but it seems weird the
245
+ * url is going to keep saying I do not have access while I click around the app
246
+ * clearly having access."*
247
+ *
248
+ * Both halves of that are one bug: the failure landing is the ordinary app page
249
+ * for whoever was ALREADY signed in, so nothing on it was ever going to mention
250
+ * a hop that failed for somebody else — and the parameter then rode along on
251
+ * every link they clicked, standing as a permanent claim that they cannot use
252
+ * the app they are using.
253
+ *
254
+ * 🔴 It lives here rather than in an app because every private satellite lands
255
+ * the same refusal, and `decideSsoAutoSignIn` ALREADY reads this parameter to
256
+ * decide not to bounce (which is why the app stays usable at all). One reader
257
+ * that acts on it and one that silently leaves it on the URL is how a message
258
+ * meant for a person became a permanent query string.
259
+ *
260
+ * @param onMessage what to show — a toast, an alert bar, whatever the app has.
261
+ */
262
+ export function useSsoErrorNotice(onMessage) {
263
+ // A ref so a caller that passes an inline arrow does not re-run the effect on
264
+ // every render and report the same failure repeatedly.
265
+ const handler = useRef(onMessage);
266
+ handler.current = onMessage;
267
+ useEffect(() => {
268
+ const message = takeSsoError();
269
+ if (message)
270
+ handler.current(message);
271
+ }, []);
272
+ }
273
+ /**
274
+ * The deep link the browser is standing on right now, as a same-origin path, or
275
+ * null when there is nothing worth carrying. This is the RETURN PATH: a shared
276
+ * `/play/<name>` link opened signed out must come back to `/play/<name>`, not to
277
+ * the app index (2026-08-05). Only a real sub-path is carried — the bare index
278
+ * (`pathname === "/"`, which is also where the `?ssoError=` failure page lives)
279
+ * has no deep link to preserve, and skipping it keeps a failed attempt's URL out
280
+ * of the round trip. The server re-validates it (`sanitizeReturnPath`), so this
281
+ * side only has to CAPTURE; it never has to be the one that decides safety.
282
+ */
283
+ function currentDeepLink() {
284
+ try {
285
+ const loc = globalThis.location;
286
+ if (!loc?.pathname || loc.pathname === "/")
287
+ return null;
288
+ return `${loc.pathname}${loc.search}${loc.hash}`;
289
+ }
290
+ catch {
291
+ return null;
292
+ }
293
+ }
294
+ /**
295
+ * Begin the redirect chain. Used by BOTH doors — the automatic one and the
296
+ * button — so the attempt is recorded exactly once either way, the deep-link
297
+ * return path is carried the same way from both, and the two can never drift
298
+ * into different behavior.
299
+ */
300
+ export function startSsoSignIn(options = {}) {
301
+ const now = options.now ?? Date.now;
302
+ if (options.automatic) {
303
+ write(SSO_ATTEMPT_KEY, String(now()));
304
+ }
305
+ else {
306
+ // An explicit press is the person overriding every guard, including
307
+ // their own earlier sign-out.
308
+ clearSsoAutoState();
309
+ }
310
+ const base = options.startPath ?? SSO_START_PATH;
311
+ const deepLink = currentDeepLink();
312
+ const query = new URLSearchParams();
313
+ if (deepLink)
314
+ query.set("return", deepLink);
315
+ // Only on an explicit press — `automatic` and `switchUser` are contradictory
316
+ // by construction (see the option's doc), and the guard keeps a future caller
317
+ // from producing a hop that silently demands a password.
318
+ if (options.switchUser && !options.automatic)
319
+ query.set("switch", "1");
320
+ if (options.addAccount && !options.automatic)
321
+ query.set("add", "1");
322
+ if (options.account)
323
+ query.set("account", options.account);
324
+ // 🔴 The tab's account rides along, and it has to. The start route stashes
325
+ // the slot for the callback to land in (a registered redirect_uri cannot
326
+ // carry a query param, so the cookie is the only channel) — without this,
327
+ // re-authenticating in a second-account tab would silently mint the session
328
+ // into slot 0 and evict the primary account in the OTHER tab.
329
+ const slot = currentAccountSlot();
330
+ if (slot !== 0)
331
+ query.set(ACCOUNT_SLOT_PARAM, String(slot));
332
+ const search = query.toString();
333
+ const url = search ? `${base}${base.includes("?") ? "&" : "?"}${search}` : base;
334
+ if (options.navigate) {
335
+ options.navigate(url);
336
+ return;
337
+ }
338
+ globalThis.location?.assign(url);
339
+ }
340
+ /**
341
+ * Leave — for real, on this app AND at auth.
342
+ *
343
+ * The signed-out marker is written FIRST and for the same reason it always
344
+ * was: the landing page after the round trip is this app's index, signed out,
345
+ * and without the flag `useSsoAutoSignIn` would immediately start another hop.
346
+ * That hop would now correctly stop at auth's login form rather than loop, but
347
+ * bouncing the person to another origin the instant they signed out is not what
348
+ * pressing "Sign out" should look like.
349
+ *
350
+ * A full-page navigation, not a fetch: the browser itself has to carry cookies
351
+ * to two origins and follow the 302 chain between them.
352
+ */
353
+ export function startSsoSignOut(options = {}) {
354
+ markSsoSignedOut();
355
+ const base = options.signOutPath ?? SSO_SIGNOUT_PATH;
356
+ const query = new URLSearchParams();
357
+ if (options.allAccounts)
358
+ query.set("all", "1");
359
+ // 🔴 Name the tab's slot, or the server ends slot 0 — which is to say, it
360
+ // signs the OWNER out because a second-account tab pressed "Sign out".
361
+ const slot = currentAccountSlot();
362
+ if (slot !== 0)
363
+ query.set(ACCOUNT_SLOT_PARAM, String(slot));
364
+ const search = query.toString();
365
+ const url = search ? `${base}${base.includes("?") ? "&" : "?"}${search}` : base;
366
+ if (options.navigate) {
367
+ options.navigate(url);
368
+ return;
369
+ }
370
+ globalThis.location?.assign(url);
371
+ }
372
+ /**
373
+ * Seed the signed-out marker before any page script runs, so the app renders
374
+ * its sign-in card instead of hopping to accounts.
375
+ *
376
+ * Every satellite's browser smoke asserts that the signed-out surface renders,
377
+ * is styled, and carries the theme toggle — real gates that have caught a
378
+ * missing stylesheet and a blank root. Automatic sign-in does not delete that
379
+ * surface, it makes it a SECOND-visit screen (after a sign-out, a failed
380
+ * callback, or a hop that did not stick), so the smoke has to ask for it
381
+ * rather than assume it. This is the honest way to ask: it puts the browser in
382
+ * the exact state a real sign-out leaves it in, with nothing test-only in the
383
+ * app.
384
+ */
385
+ export async function suppressSsoAutoSignIn(target) {
386
+ await target.addInitScript((key) => {
387
+ try {
388
+ sessionStorage.setItem(key, "1");
389
+ }
390
+ catch {
391
+ /* a browser that cannot store it will not auto-start either */
392
+ }
393
+ }, SSO_SIGNED_OUT_KEY);
394
+ }
395
+ /**
396
+ * Returns `true` while the page is on its way to the accounts satellite. A
397
+ * caller renders its LOADING view for that — never the sign-in screen, which is
398
+ * the flash this whole feature exists to delete.
399
+ */
400
+ export function useSsoAutoSignIn(input) {
401
+ const { authenticated, accountsUrl, enabled, startPath } = input;
402
+ const [redirecting, setRedirecting] = useState(false);
403
+ useEffect(() => {
404
+ if (authenticated === undefined)
405
+ return;
406
+ if (authenticated) {
407
+ // A confirmed session retires both guards.
408
+ clearSsoAutoState();
409
+ return;
410
+ }
411
+ const decision = decideSsoAutoSignIn({
412
+ authenticated: false,
413
+ accountsUrl,
414
+ // 🔴 The CACHE, never the live URL. A notice rendered inside the signed-out
415
+ // branch is a CHILD, and React runs child effects first — so by the time this
416
+ // effect runs the parameter may already have been taken off the URL, and this
417
+ // guard would decide there was no failure and bounce into the loop.
418
+ ssoError: ssoErrorForThisLoad(),
419
+ signedOut: ssoSignedOutHere(),
420
+ lastAttemptAt: lastSsoAttemptAt(),
421
+ now: Date.now(),
422
+ enabled,
423
+ });
424
+ if (!decision.start)
425
+ return;
426
+ setRedirecting(true);
427
+ startSsoSignIn({ startPath, automatic: true });
428
+ }, [authenticated, accountsUrl, enabled, startPath]);
429
+ return redirecting;
430
+ }
431
+ //# sourceMappingURL=ssoAutoSignIn.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ssoAutoSignIn.js","sourceRoot":"","sources":["../src/ssoAutoSignIn.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgDG;AAEH;;+EAE+E;AAC/E,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AACpD,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAC1D,OAAO,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AAEpD,MAAM,CAAC,MAAM,cAAc,GAAG,qBAAqB,CAAC;AACpD;;;;;;;;GAQG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAG,uBAAuB,CAAC;AAExD,2EAA2E;AAC3E,MAAM,CAAC,MAAM,eAAe,GAAG,8BAA8B,CAAC;AAC9D,uEAAuE;AACvE,MAAM,CAAC,MAAM,kBAAkB,GAAG,4BAA4B,CAAC;AAE/D;;;;;;;;GAQG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAG,MAAM,CAAC;AA+B9C;;;GAGG;AACH,MAAM,UAAU,mBAAmB,CAAC,KAAmB;IACtD,IAAI,KAAK,CAAC,OAAO,KAAK,KAAK;QAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,UAAU,EAAE,CAAC;IACzE,IAAI,KAAK,CAAC,aAAa;QAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,eAAe,EAAE,CAAC;IAC1E,0EAA0E;IAC1E,IAAI,CAAC,KAAK,CAAC,WAAW;QAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,iBAAiB,EAAE,CAAC;IAC3E,uEAAuE;IACvE,IAAI,KAAK,CAAC,QAAQ;QAAE,OAAO,EAAE,KAAK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