Skip to content

Commit 1a3e730

Browse files
baxenmbot
andcommitted
Keep Replace's account across the restart and the backup prompt mounted behind the mismatch
The sign-in after Replace's restart now has to reach the account linked to the key the prompt promised to restore; another account, or one with no key, fails setup before any key is made, linked or restored, offering Use a different account. The linked-key prompt keeps BackupConsent mounted and pauses it, so a consent prompt closed earlier stays closed. Co-authored-by: mbot <3754f8729004d95654c46dbab3129e4ab9ef05cc2534e2a3fbfc155983bd637b@buzz.block.builderlab.xyz> Signed-off-by: Bradley Axen <[email protected]>
1 parent 21888f6 commit 1a3e730

8 files changed

Lines changed: 274 additions & 40 deletions

File tree

‎docs/buzz-help.md‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,12 @@ mention anyone. Explicit agent watches on that room can still receive them.
6464
account turns out to be linked to a different key, Buzz says so over the open
6565
app. When Builderlab has a backup of the account's key, **Replace** removes
6666
this computer's key and restarts Buzz to restore the account's; copy this
67-
key from Settings → Identity first if it isn't backed up. **Not now** keeps
68-
this key, and Buzz doesn't ask again about the same two keys. An existing private key can be entered from **Welcome back**.
67+
key from Settings → Identity first if it isn't backed up; if the browser
68+
then signs in to a different account, Buzz stops and offers **Use a different
69+
account**. Without such a backup, Buzz only says where the account's key is,
70+
with **OK**. **Not now** or **OK** keeps this key, and Buzz doesn't ask again
71+
about the same two keys, unless the account's key later gets a backup, when
72+
it asks again with **Replace**. An existing private key can be entered from **Welcome back**.
6973
Its **Use a different account** signs Buzz out of that account; the next
7074
sign-in asks the browser to sign in again rather than reuse the account it
7175
remembers. Signing in to a different account than the one saved on this

‎docs/identity.md‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,11 @@ device confirmed a backup of it (as **A different account is saved on this
7272
computer** does) or to copy its private key from Settings → Identity first.
7373
**Replace** runs that dialog's Replace: it signs this key out without erase
7474
and restarts, and the sign-in after the restart takes the lane for a device
75-
with no key, where Welcome back restores the account's key. With no backup of
75+
with no key, where Welcome back restores the account's key. The restart's note
76+
names that key (`buzz.sign-in-after-restart.expected.v1`): when the browser
77+
signs in to an account linked to another key, or to none, setup fails before it
78+
creates, links or restores any key, offering only **Use a different account**,
79+
which drops the expectation. With no backup of
7680
the account's key, or no backups on this build or account (`unavailable`),
7781
Replace is not offered, since Welcome back could not restore it: the dialog says the account's
7882
key lives on another device and offers only **OK**. A backup status read that
@@ -82,8 +86,9 @@ Close keep the key and are remembered for that pair of keys (the account's and
8286
this device's) and whether Replace was offered (`buzz.linked-key-dismissed.v1`),
8387
so it asks again only when either key changes or the account's key gets a
8488
backup. The prompt waits while the launch screen shows or old Buzz's
85-
communities are being offered or added, and the backup consent prompt waits
86-
while it shows.
89+
communities are being offered or added. The backup consent prompt stays
90+
mounted but waits while it shows, so a consent prompt closed earlier stays
91+
closed after it.
8792

8893
A URL given on the welcome screen is looked up first, before sign-in and before
8994
any key exists: **Continue** opens the same **Finding community** page described

‎src/features/identity/BackupConsent.tsx‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,21 @@ let answering = 0;
6868
* are offered or added, which the app starts once launch ends, so the prompt
6969
* never opens over that dialog and decides with the communities it adds.
7070
* Closing the prompt saves nothing and keeps it closed until Builderlab signs
71-
* out or the app next starts.
71+
* out or the app next starts. While it is `paused` behind another prompt,
72+
* nothing is decided or asked either.
7273
*/
7374
export function BackupConsent({
7475
identity,
7576
communities,
7677
servers = signInServers,
78+
paused = false,
7779
}: {
7880
identity: Identity;
7981
communities: Pick<CommunityReader, "snapshot" | "subscribe">;
8082
/** The build's sign-in servers, for which communities its Builderlab hosts. */
8183
servers?: () => Promise<readonly SessionSnapshot[]>;
84+
/** Another prompt is open, which this one waits behind. */
85+
paused?: boolean;
8286
}) {
8387
const status = useSyncExternalStore(
8488
identity.backup.subscribe,
@@ -89,7 +93,7 @@ export function BackupConsent({
8993
subscribeLegacyImport,
9094
legacyImportPending,
9195
);
92-
const held = launching || importing;
96+
const held = launching || importing || paused;
9397
const [asking, setAsking] = useState<string>();
9498
const dismissed = useRef(false);
9599
const waiting =

‎src/features/identity/LinkedKeyCheck.tsx‎

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export const useSession = (
6969
* where the account's key is. Not now (or OK) is remembered for this pair of
7070
* keys and whether Replace was offered, so it asks again only when either key
7171
* changes or the account's key gets a backup. While it asks, `children` (the
72-
* backup prompt) waits.
72+
* backup prompt) stays mounted, keeping what it was told, and is told to wait.
7373
*/
7474
export function LinkedKeyCheck({
7575
federated,
@@ -83,8 +83,10 @@ export function LinkedKeyCheck({
8383
identity: Identity;
8484
/** The key on this device. */
8585
viewer: string;
86-
onReplace: () => void;
87-
children?: ReactNode;
86+
/** Replace, for the account linked to `linked`. */
87+
onReplace: (linked: string) => void;
88+
/** Rendered throughout, told whether this check is asking. */
89+
children?: (asking: boolean) => ReactNode;
8890
}) {
8991
const session = useSession(federated);
9092
const revision =
@@ -140,22 +142,21 @@ export function LinkedKeyCheck({
140142
current = false;
141143
};
142144
}, [federated, identity, viewer, revision]);
143-
if (
144-
!found ||
145-
found.revision !== revision ||
146-
found.viewer !== viewer ||
147-
launching ||
148-
importing
149-
)
150-
return children;
145+
const asking =
146+
!!found &&
147+
found.revision === revision &&
148+
found.viewer === viewer &&
149+
!launching &&
150+
!importing;
151+
if (!asking) return children?.(false);
151152
const dismiss = () => {
152153
const question = asked(found.linked, found.viewer, found.restorable);
153154
dismissed.current = question;
154155
rememberDismissed(question);
155156
setFound(undefined);
156157
};
157158
const account = session?.email ?? "Your Builderlab account";
158-
return found.restorable ? (
159+
const prompt = found.restorable ? (
159160
<AlertDialog
160161
title="Your account uses a different key"
161162
description={`${account} is linked to a different key than the one on this computer, and Builderlab has a backup of it. Replacing removes this computer’s key and restarts Buzz to restore your account’s key. ${
@@ -167,7 +168,7 @@ export function LinkedKeyCheck({
167168
actions={
168169
<>
169170
<Button onClick={dismiss}>Not now</Button>
170-
<Button variant="destructive" onClick={onReplace}>
171+
<Button variant="destructive" onClick={() => onReplace(found.linked)}>
171172
Replace
172173
</Button>
173174
</>
@@ -181,4 +182,10 @@ export function LinkedKeyCheck({
181182
actions={<Button onClick={dismiss}>OK</Button>}
182183
/>
183184
);
185+
return (
186+
<>
187+
{children?.(true)}
188+
{prompt}
189+
</>
190+
);
184191
}

‎src/features/identity/Welcome.tsx‎

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,17 @@ import { signOutFailure } from "./SignOut";
9797
import {
9898
onSignInRequest,
9999
requestSignInAfterRestart,
100+
takeExpectedKey,
100101
takeSignInRequest,
101102
} from "./sign-in-requests";
102103
import { WelcomeBack } from "./WelcomeBack";
103104
import { WireframeScreen } from "./Wireframe";
104105

105106
/** How long a replaced key's sign-out may take to restart Buzz before setup says so. */
106107
const RESTART_WAIT_MS = 30_000;
108+
/** The sign-in after Replace's restart reached an account other than the one it replaced the key for. */
109+
const WRONG_ACCOUNT =
110+
"You signed in to a different Builderlab account than the one whose key Buzz restarted to restore. Use a different account to sign in to that one.";
107111

108112
/**
109113
* The signed-in account: the development seed when a dev server set one
@@ -317,11 +321,17 @@ export function Welcome({
317321
// "Open Buzz" after email verification (`sign-in-requests.ts`). Each render
318322
// sets what a request does now; while it can't act yet, it stays queued.
319323
const requestedSignIn = useRef<(() => void) | undefined>(undefined);
324+
// The linked key a restart for Replace promised to restore: a sign-in to an
325+
// account linked to any other key, or to none, stops before setup acts on
326+
// it. Cleared on the landing.
327+
const expectedKey = useRef<string | undefined>(undefined);
320328
// biome-ignore lint/correctness/useExhaustiveDependencies: the landing appearing lets a queued request act.
321329
useEffect(() => {
322330
const handle = () => {
323331
const run = requestedSignIn.current;
324-
if (run && takeSignInRequest()) run();
332+
if (!run || !takeSignInRequest()) return;
333+
expectedKey.current = takeExpectedKey();
334+
run();
325335
};
326336
handle();
327337
return onSignInRequest(handle);
@@ -406,13 +416,14 @@ export function Welcome({
406416
);
407417
/**
408418
* Signs this device's key out, which restarts Buzz; the next launch signs in
409-
* again by itself and, with no key here, takes the account's lane. Setup
410-
* stays on its progress page until the restart, or fails if none comes.
419+
* again by itself and, with no key here, takes the lane of the account
420+
* linked to `linked`, or stops if it signs in to another. Setup stays on its
421+
* progress page until the restart, or fails if none comes.
411422
*/
412-
const replaceKey: SetupStep = {
423+
const replaceKey = (linked: string | undefined): SetupStep => ({
413424
label: "Signing in…",
414425
run: async () => {
415-
const withdraw = requestSignInAfterRestart();
426+
const withdraw = requestSignInAfterRestart(linked);
416427
try {
417428
await identity.signOut({ wipe: false, removeAgents: false });
418429
} catch (reason) {
@@ -422,7 +433,7 @@ export function Welcome({
422433
await new Promise((settle) => setTimeout(settle, RESTART_WAIT_MS));
423434
throw new Error("Buzz didn’t restart. Quit and reopen Buzz to finish.");
424435
},
425-
};
436+
});
426437
if (inApp && viewer) {
427438
// Already in the app: a request has nothing to finish, so drop it.
428439
requestedSignIn.current = () => {};
@@ -433,13 +444,16 @@ export function Welcome({
433444
federated={federatedIdentity}
434445
identity={identity}
435446
viewer={viewer}
436-
onReplace={() => void setup.start([replaceKey])}
447+
onReplace={(key) => void setup.start([replaceKey(key)])}
437448
>
438-
<BackupConsent
439-
identity={identity}
440-
communities={communities}
441-
servers={servers}
442-
/>
449+
{(asking) => (
450+
<BackupConsent
451+
identity={identity}
452+
communities={communities}
453+
servers={servers}
454+
paused={asking}
455+
/>
456+
)}
443457
</LinkedKeyCheck>
444458
</>
445459
);
@@ -466,6 +480,7 @@ export function Welcome({
466480
let checkedKey: string | undefined;
467481
let checkedBackups: readonly string[] | undefined;
468482
const signedIn = identity.snapshot();
483+
const expected = expectedKey.current;
469484
void setup.start([
470485
checkAccount(
471486
current,
@@ -478,6 +493,19 @@ export function Welcome({
478493
},
479494
signedIn.status === "ready" ? signedIn.viewer : undefined,
480495
),
496+
// Replace restarted for the account linked to `expected`: another
497+
// account, or one with no key, is neither bound, given a new key nor
498+
// restored.
499+
...(expected
500+
? [
501+
{
502+
label: "Checking your account…",
503+
run: async () => {
504+
if (checkedKey !== expected) throw new Error(WRONG_ACCOUNT);
505+
},
506+
},
507+
]
508+
: []),
481509
checkBackup(current, (keys) => {
482510
checkedBackups = keys;
483511
setBackups(keys);
@@ -721,6 +749,7 @@ export function Welcome({
721749
/** Every way back to the landing, so a kept `finding` cannot re-enter discovery. */
722750
const backToLanding = () => {
723751
lookups.current++;
752+
expectedKey.current = undefined;
724753
setOtherOptions(false);
725754
setFinding(undefined);
726755
setInviteCode("");
@@ -983,7 +1012,7 @@ export function Welcome({
9831012
<Button onClick={switchAccount}>Cancel</Button>
9841013
<Button
9851014
variant="destructive"
986-
onClick={() => void setup.start([replaceKey])}
1015+
onClick={() => void setup.start([replaceKey(linked)])}
9871016
>
9881017
Replace
9891018
</Button>
@@ -1360,7 +1389,9 @@ export function Welcome({
13601389
onBack={reads.current ? undefined : backToLanding}
13611390
onSwitchAccount={reads.current ? leaveFailedSetup : undefined}
13621391
onRetry={
1363-
progress.unsupportedBackup || progress.bound
1392+
progress.unsupportedBackup ||
1393+
progress.bound ||
1394+
progress.error === WRONG_ACCOUNT
13641395
? undefined
13651396
: () => void setup.retry()
13661397
}

0 commit comments

Comments
 (0)