diff --git a/lib/analytics/widgets/analytics_lifecycle_handler.dart b/lib/analytics/widgets/analytics_lifecycle_handler.dart index c01989dacc..96323ca98a 100644 --- a/lib/analytics/widgets/analytics_lifecycle_handler.dart +++ b/lib/analytics/widgets/analytics_lifecycle_handler.dart @@ -1,10 +1,12 @@ import 'package:flutter/widgets.dart'; +import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:get_it/get_it.dart'; import 'package:web_dex/app_config/package_information.dart'; import 'package:web_dex/bloc/analytics/analytics_repo.dart'; import 'package:web_dex/analytics/events/user_engagement_events.dart'; import 'package:web_dex/services/platform_info/plaftorm_info.dart'; import 'package:web_dex/shared/utils/utils.dart'; +import 'package:web_dex/bloc/auth_bloc/auth_bloc.dart'; /// A widget that handles analytics lifecycle events like app opened/resumed. /// @@ -53,6 +55,7 @@ class _AnalyticsLifecycleHandlerState extends State // Schedule the initial app opened event to be logged after the first frame WidgetsBinding.instance.addPostFrameCallback((_) { _logAppOpenedEvent(); + _checkAuthStatus(); }); } @@ -69,6 +72,7 @@ class _AnalyticsLifecycleHandlerState extends State // Log app opened event when app is resumed (but not on initial open) if (state == AppLifecycleState.resumed && _hasLoggedInitialOpen) { _logAppOpenedEvent(); + _checkAuthStatus(); } } @@ -100,6 +104,14 @@ class _AnalyticsLifecycleHandlerState extends State } } + void _checkAuthStatus() { + try { + context.read().add(const AuthLifecycleCheckRequested()); + } catch (e) { + log('AnalyticsLifecycleHandler: Failed to check auth status - $e'); + } + } + @override Widget build(BuildContext context) { return widget.child; diff --git a/lib/bloc/auth_bloc/auth_bloc.dart b/lib/bloc/auth_bloc/auth_bloc.dart index c7fa2a0c6d..3fe2189e6f 100644 --- a/lib/bloc/auth_bloc/auth_bloc.dart +++ b/lib/bloc/auth_bloc/auth_bloc.dart @@ -30,6 +30,7 @@ class AuthBloc extends Bloc { on(_onRestore); on(_onSeedBackupConfirmed); on(_onWalletDownloadRequested); + on(_onLifecycleCheckRequested); } final KomodoDefiSdk _kdfSdk; @@ -292,9 +293,20 @@ class AuthBloc extends Bloc { } } + Future _onLifecycleCheckRequested( + AuthLifecycleCheckRequested event, + Emitter emit, + ) async { + final currentUser = await _kdfSdk.auth.currentUser; + if (currentUser != null) { + emit(AuthBlocState.loggedIn(currentUser)); + _listenToAuthStateChanges(); + } + } + void _listenToAuthStateChanges() { _authChangesSubscription?.cancel(); - _authChangesSubscription = _kdfSdk.auth.authStateChanges.listen((user) { + _authChangesSubscription = _kdfSdk.auth.watchCurrentUser().listen((user) { final AuthorizeMode event = user != null ? AuthorizeMode.logIn : AuthorizeMode.noLogin; add(AuthModeChanged(mode: event, currentUser: user)); diff --git a/lib/bloc/auth_bloc/auth_bloc_event.dart b/lib/bloc/auth_bloc/auth_bloc_event.dart index 7692a49668..b3b38f9e12 100644 --- a/lib/bloc/auth_bloc/auth_bloc_event.dart +++ b/lib/bloc/auth_bloc/auth_bloc_event.dart @@ -53,3 +53,9 @@ class AuthWalletDownloadRequested extends AuthBlocEvent { const AuthWalletDownloadRequested({required this.password}); final String password; } + +/// Event emitted on app lifecycle changes to check if a user is already signed +/// in and restore the auth state. +class AuthLifecycleCheckRequested extends AuthBlocEvent { + const AuthLifecycleCheckRequested(); +} diff --git a/lib/bloc/bridge_form/bridge_bloc.dart b/lib/bloc/bridge_form/bridge_bloc.dart index d45152d8d7..a68ae2728c 100644 --- a/lib/bloc/bridge_form/bridge_bloc.dart +++ b/lib/bloc/bridge_form/bridge_bloc.dart @@ -78,7 +78,8 @@ class BridgeBloc extends Bloc { dexRepository: dexRepository, ); - _authorizationSubscription = _kdfSdk.auth.authStateChanges.listen((event) { + _authorizationSubscription = + _kdfSdk.auth.watchCurrentUser().listen((event) { _isLoggedIn = event != null; if (!_isLoggedIn) add(const BridgeLogout()); }); diff --git a/lib/bloc/coins_bloc/coins_bloc.dart b/lib/bloc/coins_bloc/coins_bloc.dart index 034b64f4a7..51e2cb1d2f 100644 --- a/lib/bloc/coins_bloc/coins_bloc.dart +++ b/lib/bloc/coins_bloc/coins_bloc.dart @@ -110,6 +110,11 @@ class CoinsBloc extends Bloc { ) async { emit(state.copyWith(coins: _coinsRepo.getKnownCoinsMap())); + final existingUser = await _kdfSdk.auth.currentUser; + if (existingUser != null) { + add(CoinsSessionStarted(existingUser)); + } + add(CoinsPricesUpdated()); _updatePricesTimer?.cancel(); _updatePricesTimer = Timer.periodic( diff --git a/lib/bloc/dex_tab_bar/dex_tab_bar_bloc.dart b/lib/bloc/dex_tab_bar/dex_tab_bar_bloc.dart index 6476d3262e..1bf31ebd4a 100644 --- a/lib/bloc/dex_tab_bar/dex_tab_bar_bloc.dart +++ b/lib/bloc/dex_tab_bar/dex_tab_bar_bloc.dart @@ -54,7 +54,8 @@ class DexTabBarBloc extends Bloc { ListenToOrdersRequested event, Emitter emit, ) { - _authorizationSubscription = _kdfSdk.auth.authStateChanges.listen((event) { + _authorizationSubscription = + _kdfSdk.auth.watchCurrentUser().listen((event) { if (event != null) { add(const TabChanged(0)); } diff --git a/lib/bloc/nft_transactions/bloc/nft_transactions_bloc.dart b/lib/bloc/nft_transactions/bloc/nft_transactions_bloc.dart index 1d3af77a9a..bb5b2ac07b 100644 --- a/lib/bloc/nft_transactions/bloc/nft_transactions_bloc.dart +++ b/lib/bloc/nft_transactions/bloc/nft_transactions_bloc.dart @@ -41,7 +41,7 @@ class NftTransactionsBloc extends Bloc { on(_changeFullFilter); on(_noLogin); - _authorizationSubscription = kdfSdk.auth.authStateChanges.listen((event) { + _authorizationSubscription = kdfSdk.auth.watchCurrentUser().listen((event) { final bool prevLoginState = _isLoggedIn; _isLoggedIn = event != null; diff --git a/lib/bloc/nfts/nft_main_bloc.dart b/lib/bloc/nfts/nft_main_bloc.dart index 2a15d76870..556fcc4c6d 100644 --- a/lib/bloc/nfts/nft_main_bloc.dart +++ b/lib/bloc/nfts/nft_main_bloc.dart @@ -27,7 +27,7 @@ class NftMainBloc extends Bloc { on(_onStartUpdate); on(_onStopUpdate); - _authorizationSubscription = _sdk.auth.authStateChanges.listen((event) { + _authorizationSubscription = _sdk.auth.watchCurrentUser().listen((event) { final isSignedIn = event != null; if (isSignedIn) { add(const NftMainChainUpdateRequested()); diff --git a/lib/bloc/taker_form/taker_bloc.dart b/lib/bloc/taker_form/taker_bloc.dart index 9b89d0ff91..e13297a3f2 100644 --- a/lib/bloc/taker_form/taker_bloc.dart +++ b/lib/bloc/taker_form/taker_bloc.dart @@ -69,7 +69,7 @@ class TakerBloc extends Bloc { on(_onVerifyOrderVolume); on(_onSetWalletReady); - _authorizationSubscription = kdfSdk.auth.authStateChanges.listen((event) { + _authorizationSubscription = kdfSdk.auth.watchCurrentUser().listen((event) { if (event != null && state.step == TakerStep.confirm) { add(TakerBackButtonClick()); } diff --git a/lib/bloc/trezor_connection_bloc/trezor_connection_bloc.dart b/lib/bloc/trezor_connection_bloc/trezor_connection_bloc.dart index 80b90c0664..128d6c56e1 100644 --- a/lib/bloc/trezor_connection_bloc/trezor_connection_bloc.dart +++ b/lib/bloc/trezor_connection_bloc/trezor_connection_bloc.dart @@ -19,7 +19,8 @@ class TrezorConnectionBloc super(TrezorConnectionState.initial()) { _trezorConnectionStatusListener = trezorRepo.connectionStatusStream .listen(_onTrezorConnectionStatusChanged); - _authModeListener = kdfSdk.auth.authStateChanges.listen(_onAuthModeChanged); + _authModeListener = + kdfSdk.auth.watchCurrentUser().listen(_onAuthModeChanged); on(_onConnectionStatusChange); } diff --git a/lib/bloc/trezor_init_bloc/trezor_init_bloc.dart b/lib/bloc/trezor_init_bloc/trezor_init_bloc.dart index 2354038e48..a13ec6358d 100644 --- a/lib/bloc/trezor_init_bloc/trezor_init_bloc.dart +++ b/lib/bloc/trezor_init_bloc/trezor_init_bloc.dart @@ -48,7 +48,7 @@ class TrezorInitBloc extends Bloc { on(_onSendPassphrase); on(_onAuthModeChange); - _authorizationSubscription = _kdfSdk.auth.authStateChanges.listen((user) { + _authorizationSubscription = _kdfSdk.auth.watchCurrentUser().listen((user) { add(TrezorInitUpdateAuthMode(user)); }); } diff --git a/lib/main.dart b/lib/main.dart index e592123246..98ef7269f7 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -155,8 +155,12 @@ class MyApp extends StatelessWidget { return MultiBlocProvider( providers: [ BlocProvider( - create: (_) => - AuthBloc(komodoDefiSdk, walletsRepository, SettingsRepository()), + create: (_) { + final bloc = AuthBloc( + komodoDefiSdk, walletsRepository, SettingsRepository()); + bloc.add(const AuthLifecycleCheckRequested()); + return bloc; + }, ), ], child: BetterFeedback( diff --git a/packages/komodo_ui_kit/pubspec.lock b/packages/komodo_ui_kit/pubspec.lock index 69a239efac..6131f88827 100644 --- a/packages/komodo_ui_kit/pubspec.lock +++ b/packages/komodo_ui_kit/pubspec.lock @@ -70,10 +70,10 @@ packages: dependency: transitive description: name: freezed_annotation - sha256: c87ff004c8aa6af2d531668b46a4ea379f7191dc6dfa066acd53d506da6e044b + sha256: "7294967ff0a6d98638e7acb774aac3af2550777accd8149c90af5b014e6d44d8" url: "https://pub.dev" source: hosted - version: "3.0.0" + version: "3.1.0" intl: dependency: "direct main" description: @@ -95,7 +95,7 @@ packages: description: path: "packages/komodo_defi_rpc_methods" ref: dev - resolved-ref: cda849860aac05b7951ea118cce58225c51c73b0 + resolved-ref: "9cf9e3756542edea823edf9fb81dbb8abb8186ca" url: "https://github.com/KomodoPlatform/komodo-defi-sdk-flutter.git" source: git version: "0.2.0+0" @@ -104,7 +104,7 @@ packages: description: path: "packages/komodo_defi_types" ref: dev - resolved-ref: cda849860aac05b7951ea118cce58225c51c73b0 + resolved-ref: "9cf9e3756542edea823edf9fb81dbb8abb8186ca" url: "https://github.com/KomodoPlatform/komodo-defi-sdk-flutter.git" source: git version: "0.2.0+0" @@ -113,7 +113,7 @@ packages: description: path: "packages/komodo_ui" ref: dev - resolved-ref: cda849860aac05b7951ea118cce58225c51c73b0 + resolved-ref: "9cf9e3756542edea823edf9fb81dbb8abb8186ca" url: "https://github.com/KomodoPlatform/komodo-defi-sdk-flutter.git" source: git version: "0.2.0+0" diff --git a/pubspec.lock b/pubspec.lock index a6cbf2c4e3..19e43862a8 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -525,10 +525,10 @@ packages: dependency: transitive description: name: freezed_annotation - sha256: c87ff004c8aa6af2d531668b46a4ea379f7191dc6dfa066acd53d506da6e044b + sha256: "7294967ff0a6d98638e7acb774aac3af2550777accd8149c90af5b014e6d44d8" url: "https://pub.dev" source: hosted - version: "3.0.0" + version: "3.1.0" frontend_server_client: dependency: transitive description: @@ -648,7 +648,7 @@ packages: description: path: "packages/komodo_cex_market_data" ref: dev - resolved-ref: cda849860aac05b7951ea118cce58225c51c73b0 + resolved-ref: "9cf9e3756542edea823edf9fb81dbb8abb8186ca" url: "https://github.com/KomodoPlatform/komodo-defi-sdk-flutter.git" source: git version: "0.0.1" @@ -657,7 +657,7 @@ packages: description: path: "packages/komodo_coin_updates" ref: dev - resolved-ref: cda849860aac05b7951ea118cce58225c51c73b0 + resolved-ref: "9cf9e3756542edea823edf9fb81dbb8abb8186ca" url: "https://github.com/KomodoPlatform/komodo-defi-sdk-flutter.git" source: git version: "1.0.0" @@ -666,7 +666,7 @@ packages: description: path: "packages/komodo_coins" ref: dev - resolved-ref: cda849860aac05b7951ea118cce58225c51c73b0 + resolved-ref: "9cf9e3756542edea823edf9fb81dbb8abb8186ca" url: "https://github.com/KomodoPlatform/komodo-defi-sdk-flutter.git" source: git version: "0.2.0+0" @@ -675,7 +675,7 @@ packages: description: path: "packages/komodo_defi_framework" ref: dev - resolved-ref: cda849860aac05b7951ea118cce58225c51c73b0 + resolved-ref: "9cf9e3756542edea823edf9fb81dbb8abb8186ca" url: "https://github.com/KomodoPlatform/komodo-defi-sdk-flutter.git" source: git version: "0.2.0" @@ -684,7 +684,7 @@ packages: description: path: "packages/komodo_defi_local_auth" ref: dev - resolved-ref: cda849860aac05b7951ea118cce58225c51c73b0 + resolved-ref: "9cf9e3756542edea823edf9fb81dbb8abb8186ca" url: "https://github.com/KomodoPlatform/komodo-defi-sdk-flutter.git" source: git version: "0.2.0+0" @@ -693,7 +693,7 @@ packages: description: path: "packages/komodo_defi_rpc_methods" ref: dev - resolved-ref: cda849860aac05b7951ea118cce58225c51c73b0 + resolved-ref: "9cf9e3756542edea823edf9fb81dbb8abb8186ca" url: "https://github.com/KomodoPlatform/komodo-defi-sdk-flutter.git" source: git version: "0.2.0+0" @@ -702,7 +702,7 @@ packages: description: path: "packages/komodo_defi_sdk" ref: dev - resolved-ref: cda849860aac05b7951ea118cce58225c51c73b0 + resolved-ref: "9cf9e3756542edea823edf9fb81dbb8abb8186ca" url: "https://github.com/KomodoPlatform/komodo-defi-sdk-flutter.git" source: git version: "0.2.0+0" @@ -711,7 +711,7 @@ packages: description: path: "packages/komodo_defi_types" ref: dev - resolved-ref: cda849860aac05b7951ea118cce58225c51c73b0 + resolved-ref: "9cf9e3756542edea823edf9fb81dbb8abb8186ca" url: "https://github.com/KomodoPlatform/komodo-defi-sdk-flutter.git" source: git version: "0.2.0+0" @@ -727,7 +727,7 @@ packages: description: path: "packages/komodo_ui" ref: dev - resolved-ref: cda849860aac05b7951ea118cce58225c51c73b0 + resolved-ref: "9cf9e3756542edea823edf9fb81dbb8abb8186ca" url: "https://github.com/KomodoPlatform/komodo-defi-sdk-flutter.git" source: git version: "0.2.0+0" @@ -743,7 +743,7 @@ packages: description: path: "packages/komodo_wallet_build_transformer" ref: dev - resolved-ref: cda849860aac05b7951ea118cce58225c51c73b0 + resolved-ref: "9cf9e3756542edea823edf9fb81dbb8abb8186ca" url: "https://github.com/KomodoPlatform/komodo-defi-sdk-flutter.git" source: git version: "0.2.0+0" @@ -799,10 +799,10 @@ packages: dependency: transitive description: name: local_auth_darwin - sha256: "630996cd7b7f28f5ab92432c4b35d055dd03a747bc319e5ffbb3c4806a3e50d2" + sha256: "25163ce60a5a6c468cf7a0e3dc8a165f824cabc2aa9e39a5e9fc5c2311b7686f" url: "https://pub.dev" source: hosted - version: "1.4.3" + version: "1.5.0" local_auth_platform_interface: dependency: transitive description: