From 9012cf6946d71bde1fe06d1f3fb76b978f6e2af9 Mon Sep 17 00:00:00 2001 From: Dhrumil Shah Date: Sat, 15 Jul 2023 21:52:41 -0400 Subject: [PATCH] fix(mobile) - Allow sign out if server is down, or device is offline (#3275) * WIP: Allow app sign out when server cannot be reached * WIP: import logging lib * WIP: move log out up --- .../ui/profile_drawer/profile_drawer.dart | 12 +++++------ .../providers/authentication.provider.dart | 21 +++++++++++++------ .../login/ui/change_password_form.dart | 13 +++++------- 3 files changed, 25 insertions(+), 21 deletions(-) diff --git a/mobile/lib/modules/home/ui/profile_drawer/profile_drawer.dart b/mobile/lib/modules/home/ui/profile_drawer/profile_drawer.dart index 4909a74c99..4587e50fe3 100644 --- a/mobile/lib/modules/home/ui/profile_drawer/profile_drawer.dart +++ b/mobile/lib/modules/home/ui/profile_drawer/profile_drawer.dart @@ -33,14 +33,12 @@ class ProfileDrawer extends HookConsumerWidget { ?.copyWith(fontWeight: FontWeight.bold), ).tr(), onTap: () async { - bool res = await ref.watch(authenticationProvider.notifier).logout(); + await ref.watch(authenticationProvider.notifier).logout(); - if (res) { - ref.watch(backupProvider.notifier).cancelBackup(); - ref.watch(assetProvider.notifier).clearAllAsset(); - ref.watch(websocketProvider.notifier).disconnect(); - AutoRouter.of(context).replace(const LoginRoute()); - } + ref.watch(backupProvider.notifier).cancelBackup(); + ref.watch(assetProvider.notifier).clearAllAsset(); + ref.watch(websocketProvider.notifier).disconnect(); + AutoRouter.of(context).replace(const LoginRoute()); }, ); } diff --git a/mobile/lib/modules/login/providers/authentication.provider.dart b/mobile/lib/modules/login/providers/authentication.provider.dart index 2b8cc88aec..96dded5cf0 100644 --- a/mobile/lib/modules/login/providers/authentication.provider.dart +++ b/mobile/lib/modules/login/providers/authentication.provider.dart @@ -14,6 +14,7 @@ import 'package:immich_mobile/shared/services/api.service.dart'; import 'package:immich_mobile/utils/db.dart'; import 'package:immich_mobile/utils/hash.dart'; import 'package:isar/isar.dart'; +import 'package:logging/logging.dart'; import 'package:openapi/api.dart'; class AuthenticationNotifier extends StateNotifier { @@ -92,21 +93,29 @@ class AuthenticationNotifier extends StateNotifier { } } - Future logout() async { + Future logout() async { + var log = Logger('AuthenticationNotifier'); try { + + String? userEmail = Store.tryGet(StoreKey.currentUser)?.email; + + _apiService.authenticationApi + .logout() + .then((_) => log.info("Logout was successfull for $userEmail")) + .onError( + (error, stackTrace) => + log.severe("Error logging out $userEmail", error, stackTrace), + ); + await Future.wait([ - _apiService.authenticationApi.logout(), clearAssetsAndAlbums(_db), Store.delete(StoreKey.currentUser), Store.delete(StoreKey.accessToken), ]); state = state.copyWith(isAuthenticated: false); - - return true; } catch (e) { - debugPrint("Error logging out $e"); - return false; + log.severe("Error logging out $e"); } } diff --git a/mobile/lib/modules/login/ui/change_password_form.dart b/mobile/lib/modules/login/ui/change_password_form.dart index f871579488..dac0bd163a 100644 --- a/mobile/lib/modules/login/ui/change_password_form.dart +++ b/mobile/lib/modules/login/ui/change_password_form.dart @@ -75,18 +75,15 @@ class ChangePasswordForm extends HookConsumerWidget { .changePassword(passwordController.value.text); if (isSuccess) { - bool res = await ref + await ref .read(authenticationProvider.notifier) .logout(); - if (res) { - ref.read(backupProvider.notifier).cancelBackup(); - ref.read(assetProvider.notifier).clearAllAsset(); - ref.read(websocketProvider.notifier).disconnect(); + ref.read(backupProvider.notifier).cancelBackup(); + ref.read(assetProvider.notifier).clearAllAsset(); + ref.read(websocketProvider.notifier).disconnect(); - AutoRouter.of(context) - .replace(const LoginRoute()); - } + AutoRouter.of(context).replace(const LoginRoute()); } } },