@@ -97,13 +97,17 @@ import { signOutFailure } from "./SignOut";
9797import {
9898 onSignInRequest ,
9999 requestSignInAfterRestart ,
100+ takeExpectedKey ,
100101 takeSignInRequest ,
101102} from "./sign-in-requests" ;
102103import { WelcomeBack } from "./WelcomeBack" ;
103104import { WireframeScreen } from "./Wireframe" ;
104105
105106/** How long a replaced key's sign-out may take to restart Buzz before setup says so. */
106107const 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