mirror of
https://github.com/block/buzz.git
synced 2026-11-24 18:21:59 +00:00
## Problem Organization sign-in can fail with `Couldn't sign in (account-unavailable)`. That code covers several different failures, so a screenshot can't tell them apart: - the account server can't be reached at all (DNS, connection, or TLS failure, or a timeout), for example when a required VPN profile isn't applied to the app; - a redirect, since the client doesn't follow redirects, so an access-proxy login page lands here; - a 5xx from the server. ## Change - `BuilderlabAccountClient` records why a request failed as `unavailable`, in `lastUnavailableCause`, and logs it. It follows the existing `lastRejectedRequest` pattern. The cause is one of: - a transport category: `unreachable`, `insecure`, `timedOut` (reusing `transportFailure`), or `network` as the fallback; - the status code, such as `http302`. - `OrganizationSignIn` saves the cause at the moment the exchange fails, so later account requests can't change it. It adds the cause to the failure code, for example `account-unavailable-http302` or `account-unavailable-unreachable`. Each new attempt clears it. - The organization sign-in page now ends its failure message with advice for that cause instead of "Try again.": - unreachable, timeout, TLS failure, redirect: "Check your network or VPN connection, then try again." - a 5xx: "The sign-in service is unavailable. Try again later." - The in-app banner keeps "Try again.": its capsule clips longer messages at default text size, and the existing banner layout test caught that. - Unexpected statuses are classified as soon as the headers arrive, so a slow or oversized error body can't hide the status. | Before | After | |---|---| |  |  | The log line contains only the host, route, and cause. It has no headers, credentials, or response bodies. ## Validation - A new journey test in `organization_sign_in_test.dart` covers an exchange that fails with a 302, a 503, and a client exception, then a successful retry. With the cause capture removed, the test fails (`Expected: 'account-unavailable-http302'`, `Actual: 'account-unavailable'`). - The same test checks the advice for each cause. The internal-build landing tests pass with `--dart-define=BUZZ_DISTRIBUTION=internal`. - The screenshots come from the real `PairingPage` rendered in a scratch widget test with a 390×844 surface, cropped. The scratch test isn't committed. - `just mobile-check` passes, and the full `flutter test` run passes: +5018, 71 skipped. - Not yet done: a device run that reproduces an unreachable account server and captures the new message on screen. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Signed-off-by: Diem Nguyen <diem@squareup.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
166 lines
5.8 KiB
Dart
166 lines
5.8 KiB
Dart
import 'dart:async';
|
||
|
||
import 'package:flutter/material.dart';
|
||
import 'package:flutter_hooks/flutter_hooks.dart';
|
||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||
|
||
import 'features/onboarding/self_hosted_entry_sheet.dart';
|
||
import 'features/pairing/pairing_page.dart';
|
||
import 'shared/auth/auth_provider.dart';
|
||
import 'shared/community/community.dart';
|
||
import 'shared/community/community_provider.dart';
|
||
import 'shared/hosted/account_identity_store.dart';
|
||
import 'shared/hosted/hosted_relay_auth.dart';
|
||
import 'shared/hosted/organization_environment.dart';
|
||
import 'shared/hosted/organization_sign_in.dart';
|
||
import 'shared/hosted/organization_identity.dart';
|
||
|
||
/// An internal build's landing: one "Sign in" that goes to the organization's
|
||
/// single sign-on, then joins the organization's community.
|
||
class OrganizationAppEntry extends HookConsumerWidget {
|
||
const OrganizationAppEntry({super.key});
|
||
|
||
@override
|
||
Widget build(BuildContext context, WidgetRef ref) {
|
||
final signIn = ref.watch(organizationSignInProvider)!;
|
||
useListenable(signIn);
|
||
final joining = useState(false);
|
||
final joinFailed = useState<_JoinFailure?>(null);
|
||
// Set by a tap, so restoring a saved session never joins on its own.
|
||
final joinRequested = useRef(false);
|
||
final relay = 'wss://${OrganizationEnvironment.relayHosts.first}';
|
||
|
||
Future<void> join() async {
|
||
if (joining.value) return;
|
||
joinRequested.value = false;
|
||
joining.value = true;
|
||
joinFailed.value = null;
|
||
bool current() => context.mounted;
|
||
final entry = ref.read(selfHostedEntryProvider);
|
||
final account = ref.read(organizationAccountProvider)!;
|
||
final storage = ref.read(communityStorageProvider);
|
||
try {
|
||
final bound = await organizationBoundPubkey(account);
|
||
if (!current()) return;
|
||
Community community;
|
||
if (bound != null) {
|
||
// The relay admits only the bound key: use this device's copy, or
|
||
// the organization's backup of it. Never offer or create another.
|
||
try {
|
||
community = await entry.prepare(
|
||
relay: relay,
|
||
name: '',
|
||
savedPubkey: bound,
|
||
isCurrent: current,
|
||
);
|
||
} on AccountIdentityFailure catch (error) {
|
||
if (error != AccountIdentityFailure.absent) rethrow;
|
||
final nsec = await recoverOrganizationKey(account, bound);
|
||
if (!current()) return;
|
||
if (nsec == null) {
|
||
joinFailed.value = _JoinFailure.notRestored;
|
||
return;
|
||
}
|
||
community = await entry.prepare(
|
||
relay: relay,
|
||
name: '',
|
||
nsec: nsec,
|
||
isCurrent: current,
|
||
);
|
||
}
|
||
} else {
|
||
try {
|
||
community = await entry.prepare(
|
||
relay: relay,
|
||
name: '',
|
||
create: true,
|
||
isCurrent: current,
|
||
);
|
||
} on AccountIdentityFailure catch (error) {
|
||
// Saved identities exist: the member picks one or a new key, which
|
||
// the organization binds at its first assertion.
|
||
if (error != AccountIdentityFailure.absent || !context.mounted) {
|
||
rethrow;
|
||
}
|
||
await showSelfHostedEntry(context, relay: relay, name: '');
|
||
return;
|
||
}
|
||
}
|
||
if (!current()) return;
|
||
await ref
|
||
.read(authProvider.notifier)
|
||
.authenticateWithCommunity(community, shouldContinue: current);
|
||
// Admission can return early after cancellation. Check the durable
|
||
// record before retiring the only retry copy of a generated key.
|
||
final saved = await storage.loadAll();
|
||
if (saved.any(
|
||
(item) => item.id == community.id && item.nsec == community.nsec,
|
||
)) {
|
||
await entry.complete(community);
|
||
}
|
||
} catch (error) {
|
||
debugPrint('Organization community entry failed: $error');
|
||
if (context.mounted) joinFailed.value = _JoinFailure.failed;
|
||
} finally {
|
||
if (context.mounted) joining.value = false;
|
||
}
|
||
}
|
||
|
||
// This landing shows only without the organization's community, so the
|
||
// member has signed out: end the session rather than reuse it.
|
||
useEffect(() {
|
||
unawaited(signIn.signOut());
|
||
return null;
|
||
}, [signIn]);
|
||
|
||
useEffect(() {
|
||
if (joinRequested.value &&
|
||
signIn.status == OrganizationSessionStatus.signedIn) {
|
||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||
if (context.mounted) unawaited(join());
|
||
});
|
||
}
|
||
if (signIn.status == OrganizationSessionStatus.signedOut) {
|
||
joinRequested.value = false;
|
||
}
|
||
return null;
|
||
}, [signIn.status]);
|
||
|
||
final busy =
|
||
joining.value ||
|
||
signIn.status == OrganizationSessionStatus.checking ||
|
||
signIn.status == OrganizationSessionStatus.signingIn;
|
||
final status =
|
||
switch (joinFailed.value) {
|
||
_JoinFailure.failed =>
|
||
'Couldn’t connect to your organization. Try again.',
|
||
_JoinFailure.notRestored =>
|
||
'Couldn’t restore your organization identity on this device. '
|
||
'Sign in on a device that has it, then try again.',
|
||
null => null,
|
||
} ??
|
||
switch (signIn.failureCode) {
|
||
null => null,
|
||
final code => 'Couldn’t sign in ($code). ${signIn.failureAdvice}',
|
||
};
|
||
|
||
return PairingPage(
|
||
connectionLanding: true,
|
||
showConnectionOptions: false,
|
||
accountStatus: status,
|
||
signInBusy: busy,
|
||
onSignIn: () {
|
||
if (signIn.status == OrganizationSessionStatus.signedIn) {
|
||
unawaited(join());
|
||
return;
|
||
}
|
||
joinRequested.value = true;
|
||
joinFailed.value = null;
|
||
unawaited(signIn.signIn());
|
||
},
|
||
);
|
||
}
|
||
}
|
||
|
||
enum _JoinFailure { failed, notRestored }
|