Files
buzz/mobile/lib/shared/auth/auth_provider.dart
klopez4212 8c0f42e187 Add mobile Huddles voice MVP (#6056)
## Summary
- add foreground mobile Huddles on Android and iOS with native Opus
capture/playback, mute, speaker routing, participants, lifecycle, and
minimized drawer UI
- keep mobile Huddle cards and roster state live, including ended rooms,
relay-resolved profiles, and agents
- broadcast desktop agent TTS through the existing Huddle audio protocol

## Scope
Foreground human-to-human voice MVP only. Agent setup/transcripts,
background calling, recording, and advanced device controls remain out
of scope.

## Validation
- `just mobile-check`
- `just mobile-test` — 1,500 passed
- `just desktop-check` and `just desktop-test` — 4,957 passed
- desktop typecheck, strict Clippy, and Tauri tests — 2,445 passed, 15
ignored
- mobile worktree identity contract checks
- physical Pixel/iPhone behavior reviewed during development

---------

Signed-off-by: kenny lopez <klopez4212@gmail.com>
Signed-off-by: Kenny Lopez <klopez4212@gmail.com>
Signed-off-by: Princess Donut <b238ea756dee4d98afa5883fc7f1de61eeabe65bf700e3a5a5a80db5e42e2c2b@buzz.block.builderlab.xyz>
Signed-off-by: Tom Brow <tomb@block.xyz>
Co-authored-by: Carl <3c4caeafb646d23867f1c4832e68211d77e2561946171625f75c3ce1a3f2670f@buzz.block.builderlab.xyz>
Co-authored-by: Princess Donut <b238ea756dee4d98afa5883fc7f1de61eeabe65bf700e3a5a5a80db5e42e2c2b@buzz.block.builderlab.xyz>
Co-authored-by: leader <71e9f2c44a6932b6772caaaccda1911d010463c3e2c6c40410b8329956046801@buzz.block.builderlab.xyz>
Co-authored-by: Tom Brow <tomb@block.xyz>
Co-authored-by: Mongo <9cfd347903944d5b85aa6c93d2ab67381b978a92a31914bca69998968752a1d7@buzz.block.builderlab.xyz>
2026-08-22 05:28:16 -07:00

118 lines
4.0 KiB
Dart

import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:nostr/nostr.dart' as nostr;
import '../community/community.dart';
import '../community/community_provider.dart';
enum AuthStatus { unknown, unauthenticated, authenticated }
class AuthState {
final AuthStatus status;
final Community? community;
const AuthState({required this.status, this.community});
}
/// Restores the active community without making connectivity load-bearing.
/// The relay session owns connection recovery after startup.
class AuthNotifier extends AsyncNotifier<AuthState> {
@override
Future<AuthState> build() async {
// Read from storage directly — NOT from community providers.
// Watching community providers here would create a circular dependency
// because authenticateWithCommunity() writes to those providers.
final storage = ref.read(communityStorageProvider);
final communities = await storage.loadAll();
if (communities.isEmpty) {
return const AuthState(status: AuthStatus.unauthenticated);
}
var activeId = await storage.loadActiveId();
while (communities.isNotEmpty) {
final active =
activeId != null && communities.any((w) => w.id == activeId)
? communities.firstWhere((w) => w.id == activeId)
: communities.first;
await storage.saveActiveId(active.id);
if (_hasValidNsec(active.nsec)) {
return AuthState(status: AuthStatus.authenticated, community: active);
}
await storage.remove(active.id);
communities.removeWhere((community) => community.id == active.id);
activeId = null;
ref.invalidate(communityListProvider);
ref.invalidate(activeCommunityProvider);
}
await storage.clearActiveId();
return const AuthState(status: AuthStatus.unauthenticated);
}
/// Authenticate with a community. Saves it and switches to it.
/// Writes to storage directly to avoid circular dependency with community
/// providers.
Future<void> authenticateWithCommunity(Community community) {
return ref.read(communityTransitionProvider).runExclusive(() async {
await ref.read(communityTransitionProvider).run();
final storage = ref.read(communityStorageProvider);
await storage.save(community);
await storage.saveActiveId(community.id);
// Invalidate community providers so other consumers pick up the new data.
ref.invalidate(communityListProvider);
ref.invalidate(activeCommunityProvider);
state = AsyncData(
AuthState(status: AuthStatus.authenticated, community: community),
);
});
}
Future<void> signOut() {
return ref.read(communityTransitionProvider).runExclusive(() async {
await ref.read(communityTransitionProvider).run();
final storage = ref.read(communityStorageProvider);
final activeId = await storage.loadActiveId();
if (activeId != null) {
await storage.remove(activeId);
await storage.clearActiveId();
}
// Check if other communities remain — switch to the next one instead of
// forcing the user back to the pairing screen.
final remaining = await storage.loadAll();
// Invalidate community providers so other consumers pick up the change.
ref.invalidate(communityListProvider);
ref.invalidate(activeCommunityProvider);
if (remaining.isNotEmpty) {
final next = remaining.first;
await storage.saveActiveId(next.id);
// Re-run build() to validate the next community's credentials.
ref.invalidateSelf();
await future;
} else {
state = const AsyncData(AuthState(status: AuthStatus.unauthenticated));
}
});
}
}
bool _hasValidNsec(String? nsec) {
if (nsec == null || nsec.isEmpty) return false;
try {
final decoded = nostr.Nip19.decode(payload: nsec);
return decoded.prefix == nostr.Nip19Prefix.nsec &&
decoded.data.length == 64;
} catch (_) {
return false;
}
}
final authProvider = AsyncNotifierProvider<AuthNotifier, AuthState>(
AuthNotifier.new,
);