Files
buzz/mobile/lib/organization_app_entry.dart
Tom Brow d39545ebd4 fix(mobile): name the failing step in sign-in errors (#9263)
Organization sign-in shows the same "Couldn't sign in. Try again."
(landing page) or "Sign in failed. Try again." (in-app banner) for every
failure after the browser hands control back: a rejected or stale
callback, a keychain write that doesn't stick, a code exchange the
server refuses, a session for a different account, or a malformed
response. Server logs can't tell these apart either, because the
exchange can succeed on the server and still fail on the device. So a
user's screenshot of the error tells us nothing about which step broke.

This appends a short code naming the failure, for example "Couldn't sign
in (account-storage). Try again." or "Sign in failed
(browser-restartLogin). Try again." The code is the failure's enum name,
prefixed by where it came from (`browser-` for the callback, `account-`
for the exchange and session). An unexpected failure type throws instead
of showing a made-up code.

### Validation

- Focused test changes: the sign-in banner's recovery test now checks
the code in the message for two account failures and one browser
failure, at each text scale.
- Full mobile suite (`flutter test`) and `flutter analyze` pass.
- Not yet: an automated app run reproducing a failed sign-in to capture
the new message on screen.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Signed-off-by: Tom Brow <tomb@squareup.com>
Co-authored-by: Tom Brow <tomb@squareup.com>
Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@buzz.block.builderlab.xyz>
2026-11-16 13:21:57 -08:00

166 lines
5.8 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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). Try again.',
};
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 }