Skip to content

Commit a9fe6ea

Browse files
baxenmbot
andcommitted
Open the app with a saved key; check the account's key in the background
A device set up through "Sign in with Builderlab" stopped at "Sign in / Continue without signing in" on every launch, even with a valid saved Builderlab session, and Sign in reopened the browser. A key on the device now opens the app, whatever the setup route, and the buzz.setup-route.v1 note is gone. Once the saved session is signed in, LinkedKeyCheck reads the account's linked key (the read Settings makes) with the app already open. A match, no linked key, no session or a failed read does nothing. Another key opens a dismissable prompt whose Replace is setup's existing replace step: sign this key out, restart, and sign in into Welcome back. The welcome screen leads with Sign in when a Builderlab session is signed in or needs reauthentication, instead of reading the note. A failed setup's Use a different account still returns to the landing with its key for that session only. Co-authored-by: mbot <3754f8729004d95654c46dbab3129e4ab9ef05cc2534e2a3fbfc155983bd637b@buzz.block.builderlab.xyz> Signed-off-by: Bradley Axen <[email protected]>
1 parent 9cc2881 commit a9fe6ea

6 files changed

Lines changed: 502 additions & 258 deletions

File tree

‎docs/buzz-help.md‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ mention anyone. Explicit agent watches on that room can still receive them.
4242

4343
- Buzz uses a key-based identity. On first launch, the welcome screen offers
4444
**Create account** and **Sign in**, which open the Builderlab account page;
45-
once this computer was set up through Builderlab, **Sign in** comes first.
45+
when this computer already has a Builderlab sign-in saved, **Sign in** comes first.
4646
**Other options** reveals a **Community URL** field
4747
for an address or invite link; desktop does not offer QR scanning.
4848
With no communities yet, **Join** advances through full-page access and profile
@@ -59,9 +59,11 @@ mention anyone. Explicit agent watches on that room can still receive them.
5959
or hide it.
6060
After Builderlab sign-in, an account with no linked key is set up
6161
automatically: Buzz creates a key (or uses the one already on this device)
62-
and links it to the signed-in account. A device set up through Builderlab asks
63-
you to sign in again on relaunch; **Continue without signing in** opens Buzz
64-
without it. An existing private key can be entered from **Welcome back**.
62+
and links it to the signed-in account. Once a key is on this computer, Buzz
63+
opens straight to the app and reuses the saved Builderlab sign-in. If that
64+
account turns out to be linked to a different key, Buzz asks over the open app:
65+
**Replace** restarts Buzz to restore the account's key, and **Not now** keeps
66+
this one. An existing private key can be entered from **Welcome back**.
6567
Its **Use a different account** signs Buzz out of that account; the next
6668
sign-in asks the browser to sign in again rather than reuse the account it
6769
remembers. Signing in to a different account than the one saved on this

‎docs/identity.md‎

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,23 @@ installation's grant, so this works whether or not the plugin has started.
5656
Welcome back offers a restore where the portal accepts the session there. The
5757
session outlives first run: sign-in at launch finds it signed in.
5858

59+
A launch with a key on the device opens the app and never signs in again for
60+
it, however the device was set up. The welcome screen leads with **Sign in**
61+
rather than **Create account** when the Builderlab session is signed in or
62+
needs reauthentication. With the app open, once that session is signed in
63+
(restored at launch, or signed in later), `LinkedKeyCheck` reads the account's
64+
linked key with the same call Settings → Identity makes, one read per session
65+
revision. A matching key, an account with no key, a refused or absent session
66+
and a failed read change nothing. Another key opens **Your account uses a
67+
different key** over the app, which says whether this device confirmed a
68+
backup of its own key (as **A different account is saved on this computer**
69+
does). **Replace** runs that dialog's Replace: it signs this key out without
70+
erase and restarts, and the sign-in after the restart takes the lane for a
71+
device with no key, where Welcome back restores the account's key. **Not now**
72+
and Close keep the key and read nothing more until the next launch. The
73+
prompt waits while the launch screen shows or old Buzz's communities are being
74+
offered or added, and the backup consent prompt waits while it shows.
75+
5976
A URL given on the welcome screen is looked up first, before sign-in and before
6077
any key exists: **Continue** opens the same **Finding community** page described
6178
below, which sends only the two unauthenticated GETs, so a typo, an unreachable
@@ -159,8 +176,8 @@ discards the draft. A successful account-backed import links the key and automat
159176
unless that key already has a backup. An import without Builderlab
160177
sign-in skips linking and backup. Importing without a Builderlab account never uploads the key.
161178

162-
**Another account's key on this device** (a relaunch sign-in as someone else)
163-
is never replaced without asking: **A different account is saved on this
179+
**Another account's key on this device** (a sign-in as someone else with a key
180+
already here) is never replaced without asking: **A different account is saved on this
164181
computer** asks first. **Replace** signs it out, as **Sign out of Buzz** does
165182
without erase; Buzz restarts and signs in again by itself (a one-time note
166183
honoured for two minutes), now on the lane for a device with no key. **Cancel**
@@ -275,14 +292,14 @@ account, **Continue without backup** skips only that backup step and runs the
275292
remaining community setup. It retains the saved linked key and does not persist
276293
a backup opt-out. Transient backup failures remain retry-only; linking cannot
277294
be bypassed.
278-
After a Builderlab sign-in, including the one a relaunch waits on, the page
279-
offers **Use a different account** in place of Back: it signs out of the
280-
account and returns to the welcome screen, keeping any key here, as a relaunch
281-
shows it. Back alone would sign in to the same account again from the
282-
browser's session. That screen's **Sign in** asks for credentials and checks
283-
the key with the next account, and **Continue without signing in** opens the
284-
app with a key already here, as on any relaunch; the account is checked at the
285-
next launch. A link kgoose refuses because the key or the account is already
295+
After a Builderlab sign-in, the page offers **Use a different account** in
296+
place of Back: it signs out of the account and returns to the welcome screen,
297+
keeping any key here. Back alone would sign in to the same account again from
298+
the browser's session. With a key here, that screen offers **Sign in**, which
299+
asks for credentials and checks the key with the next account, and **Continue
300+
without signing in**, which opens the app with that key. This is the only way a
301+
key on the device reaches the welcome screen, and nothing about it is saved:
302+
the next launch opens the app. A link kgoose refuses because the key or the account is already
286303
bound elsewhere (`pubkey_already_bound`, `identity_already_bound`) is not
287304
offered Try again, which would get the same answer; Use a different account is
288305
the only way on. Failures before any sign-in keep **Back**.
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
import {
2+
type ReactNode,
3+
useEffect,
4+
useRef,
5+
useState,
6+
useSyncExternalStore,
7+
} from "react";
8+
import { launchVisible, subscribeLaunch } from "../../app/launch";
9+
import { AlertDialog } from "../../shared/design-system/ui/AlertDialog";
10+
import { Button } from "../../shared/design-system/ui/Button";
11+
import {
12+
legacyImportPending,
13+
subscribeLegacyImport,
14+
} from "../communities/legacy-communities";
15+
import type {
16+
FederatedIdentity,
17+
SessionSnapshot,
18+
} from "../federated-identity/service";
19+
import { federatedBuilderlabAccount } from "./builderlab-account";
20+
import type { Identity } from "./service";
21+
22+
type Session = Pick<FederatedIdentity, "snapshot" | "subscribe" | "request">;
23+
24+
const noSession = () => () => {};
25+
const noSnapshot = (): SessionSnapshot | undefined => undefined;
26+
27+
/** The build's Builderlab session, or `undefined` for a build without one. */
28+
export const useSession = (
29+
federated: Pick<FederatedIdentity, "snapshot" | "subscribe"> | undefined,
30+
) =>
31+
useSyncExternalStore(
32+
federated?.subscribe ?? noSession,
33+
federated?.snapshot ?? noSnapshot,
34+
);
35+
36+
/**
37+
* Checks, with the app already open, that the Builderlab account signed in
38+
* here is linked to this device's key. Launch never waits on it: once the
39+
* saved session is signed in, it reads the account's linked key, the read
40+
* Settings makes, and asks only when that is another key. No linked key, a
41+
* refused or absent session, or a failed read leaves the app as it is.
42+
* Replace is setup's own replace step, which signs this key out and restarts
43+
* into the account's Welcome back. Not now keeps the key and asks again at
44+
* the next launch. While it asks, `children` (the backup prompt) waits.
45+
*/
46+
export function LinkedKeyCheck({
47+
federated,
48+
identity,
49+
viewer,
50+
onReplace,
51+
children,
52+
}: {
53+
/** The build's Builderlab session; without one there is nothing to check. */
54+
federated?: Session | undefined;
55+
identity: Identity;
56+
/** The key on this device. */
57+
viewer: string;
58+
onReplace: () => void;
59+
children?: ReactNode;
60+
}) {
61+
const session = useSession(federated);
62+
const revision =
63+
session?.status === "signed-in" && session.authority
64+
? session.revision
65+
: undefined;
66+
const launching = useSyncExternalStore(subscribeLaunch, launchVisible);
67+
const importing = useSyncExternalStore(
68+
subscribeLegacyImport,
69+
legacyImportPending,
70+
);
71+
// The mismatch found, for the session and key it was read with.
72+
const [found, setFound] = useState<{
73+
revision: string;
74+
viewer: string;
75+
backedUp: boolean;
76+
}>();
77+
const dismissed = useRef(false);
78+
useEffect(() => {
79+
if (!federated || !revision || dismissed.current) return;
80+
let current = true;
81+
// A build that can't make the account (no Builderlab URL) has nothing to check.
82+
Promise.resolve()
83+
.then(() =>
84+
federatedBuilderlabAccount(
85+
{ revision },
86+
federated,
87+
identity,
88+
).linkedKey(),
89+
)
90+
.then(async (linked) => {
91+
if (!current || !linked || linked === viewer) return;
92+
const backedUp = await identity.backedUp(viewer).catch(() => false);
93+
if (current) setFound({ revision, viewer, backedUp });
94+
})
95+
.catch(() => {});
96+
return () => {
97+
current = false;
98+
};
99+
}, [federated, identity, viewer, revision]);
100+
if (
101+
!found ||
102+
found.revision !== revision ||
103+
found.viewer !== viewer ||
104+
launching ||
105+
importing
106+
)
107+
return children;
108+
const dismiss = () => {
109+
dismissed.current = true;
110+
setFound(undefined);
111+
};
112+
return (
113+
<AlertDialog
114+
title="Your account uses a different key"
115+
description={`${session?.email ?? "Your Builderlab account"} is linked to a different key than the one on this computer. Replace it to use your account’s key: Buzz restarts and signs you in again. ${
116+
found.backedUp
117+
? "This key was backed up in Builderlab: sign in to its account to restore it."
118+
: "This key may not be backed up."
119+
}`}
120+
onClose={dismiss}
121+
actions={
122+
<>
123+
<Button onClick={dismiss}>Not now</Button>
124+
<Button variant="destructive" onClick={onReplace}>
125+
Replace
126+
</Button>
127+
</>
128+
}
129+
/>
130+
);
131+
}

0 commit comments

Comments
 (0)