Skip to content

Commit

Permalink
TW-1785: Improve logout for multiple homeserver
Browse files Browse the repository at this point in the history
  • Loading branch information
nqhhdev committed May 21, 2024
1 parent 615ddd8 commit 39f8d40
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 22 deletions.
42 changes: 38 additions & 4 deletions lib/pages/settings_dashboard/settings/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,40 @@ class SettingsController extends State<Settings> with ConnectPageMixin {
OkCancelResult.cancel) {
return;
}
final matrix = Matrix.of(context);
if (PlatformInfos.isMobile) {
await _logoutActionsOnMobile();
} else {
await _logoutActionsOnWeb();
}
}

Future<void> _logoutActionsOnMobile() async {
try {
await tryLogoutSso(context);
} catch (e) {
Logs().e('SettingsController()::_logoutActionsOnMobile - error: $e');
return;
}
if (matrix.canDeleteToMDatabase == true) {
final hiveCollectionToMDatabase = getIt.get<HiveCollectionToMDatabase>();
await hiveCollectionToMDatabase.clear();
}
if (matrix.twakeSupported == true) {
await TwakeDialog.showFutureLoadingDialogFullScreen(
future: () async {
try {
if (matrix.backgroundPush != null) {
await matrix.backgroundPush!.removeCurrentPusher();
}
await matrix.client.logout();
} catch (e) {
Logs().e('SettingsController()::_logoutActionsOnMobile - error: $e');
}
},
);
}

Future<void> _logoutActionsOnWeb() async {
if (matrix.canDeleteToMDatabase == true) {
final hiveCollectionToMDatabase = getIt.get<HiveCollectionToMDatabase>();
await hiveCollectionToMDatabase.clear();
}
Expand All @@ -102,10 +131,15 @@ class SettingsController extends State<Settings> with ConnectPageMixin {
}
await matrix.client.logout();
} catch (e) {
Logs().e('SettingsController()::logoutAction - error: $e');
Logs().e('SettingsController()::_logoutActionsOnWeb - error: $e');
} finally {
if (PlatformInfos.isWeb) {
await tryLogoutSso(context);
try {
await tryLogoutSso(context);
} catch (e) {
Logs().e('SettingsController()::_logoutActionsOnWeb - error: $e');
return;
}
}
}
},
Expand Down
1 change: 1 addition & 0 deletions lib/presentation/mixins/connect_page_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ mixin ConnectPageMixin {
Logs().d('tryLogoutSso::result: $result');
} catch (e) {
Logs().d('tryLogoutSso::error: $e');
rethrow;
}
}

Expand Down
61 changes: 43 additions & 18 deletions lib/widgets/matrix.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class MatrixState extends State<Matrix>
String? activeBundle;
Store store = Store();
HomeserverSummary? loginHomeserverSummary;
String? authUrl;
String? _authUrl;
XFile? loginAvatar;
String? loginUsername;
LoginType? loginType;
Expand All @@ -94,8 +94,16 @@ class MatrixState extends State<Matrix>
return tomServerUrlInterceptor.baseUrl != null;
}

bool get canDeleteToMDatabase {
final lastClientTwakeSupport =
widget.clients.where((client) => client.homeserver != null).length == 1;
return lastClientTwakeSupport && twakeSupported;
}

BackgroundPush? backgroundPush;

String? get authUrl => _authUrl;

Client get client {
if (widget.clients.isEmpty) {
widget.clients.add(getLoginClient());
Expand Down Expand Up @@ -125,7 +133,7 @@ class MatrixState extends State<Matrix>
_activeClient = index;
// TODO: Multi-client VoiP support
createVoipPlugin();
_setUpToMServicesWhenChangingActiveClient(newClient);
await _setUpToMServicesWhenChangingActiveClient(newClient);
await _storePersistActiveAccount(newClient!);
return SetActiveClientState.success;
} else {
Expand Down Expand Up @@ -392,7 +400,7 @@ class MatrixState extends State<Matrix>
) async {
await _cancelSubs(currentClient.clientName);
widget.clients.remove(currentClient);
ClientManager.removeClientNameFromStore(currentClient.clientName);
await ClientManager.removeClientNameFromStore(currentClient.clientName);
TwakeSnackBar.show(
TwakeApp.routerKey.currentContext!,
L10n.of(context)!.oneClientLoggedOut,
Expand All @@ -411,8 +419,8 @@ class MatrixState extends State<Matrix>
}
}

void _handleFirstLoggedIn(Client newActiveClient) {
setUpToMServicesInLogin(newActiveClient);
Future<void> _handleFirstLoggedIn(Client newActiveClient) async {
await setUpToMServicesInLogin(newActiveClient);
_storePersistActiveAccount(newActiveClient);
TwakeApp.router.go(
'/rooms',
Expand All @@ -439,7 +447,7 @@ class MatrixState extends State<Matrix>
_loginClientCandidate!.clientName,
);
if (activeClient == null) return;
setUpToMServicesInLogin(activeClient);
await setUpToMServicesInLogin(activeClient);
final result = await setActiveClient(activeClient);
if (result.isSuccess) {
TwakeApp.router.go(
Expand Down Expand Up @@ -566,7 +574,7 @@ class MatrixState extends State<Matrix>
toMConfigurations.tomServerInformation,
toMConfigurations.identityServerInformation,
);
authUrl = toMConfigurations.authUrl;
_setupAuthUrl(url: toMConfigurations.authUrl);
loginType = toMConfigurations.loginType;
} catch (e) {
Logs().e('MatrixState::_retrieveToMConfiguration: $e');
Expand All @@ -588,7 +596,7 @@ class MatrixState extends State<Matrix>
}
}

void setUpToMServicesInLogin(Client client) {
Future<void> setUpToMServicesInLogin(Client client) async {
final tomServer = loginHomeserverSummary?.tomServer;
Logs().d('MatrixState::setUpToMServicesInLogin: $tomServer');
if (tomServer != null) {
Expand All @@ -598,17 +606,15 @@ class MatrixState extends State<Matrix>
loginHomeserverSummary?.discoveryInformation?.mIdentityServer;
final homeServer =
loginHomeserverSummary?.discoveryInformation?.mHomeserver;
final newAuthUrl = loginHomeserverSummary?.discoveryInformation
?.additionalProperties["m.authentication"]?["issuer"];
authUrl = newAuthUrl is String ? newAuthUrl : null;
_setupAuthUrl();
if (identityServer != null) {
_setUpIdentityServer(identityServer);
}
if (homeServer != null) {
_setUpHomeServer(homeServer.baseUrl);
}
if (tomServer != null) {
_storeToMConfiguration(
await _storeToMConfiguration(
client,
ToMConfigurations(
tomServerInformation: tomServer,
Expand Down Expand Up @@ -657,10 +663,10 @@ class MatrixState extends State<Matrix>
.changeBaseUrl(identityServer.baseUrl.toString());
}

void _storeToMConfiguration(
Future<void> _storeToMConfiguration(
Client client,
ToMConfigurations config,
) {
) async {
try {
Logs().e(
'Matrix::_storeToMConfiguration: clientName - ${client.clientName}',
Expand All @@ -671,7 +677,7 @@ class MatrixState extends State<Matrix>
if (client.userID == null) return;
final ToMConfigurationsRepository configurationRepository =
getIt.get<ToMConfigurationsRepository>();
configurationRepository.saveTomConfigurations(
await configurationRepository.saveTomConfigurations(
client.userID!,
config,
);
Expand All @@ -683,7 +689,7 @@ class MatrixState extends State<Matrix>
}
}

void _setUpToMServicesWhenChangingActiveClient(Client? client) async {
Future<void> _setUpToMServicesWhenChangingActiveClient(Client? client) async {
Logs().d(
'Matrix::_checkHomeserverExists: Old twakeSupported - $twakeSupported',
);
Expand All @@ -697,6 +703,7 @@ class MatrixState extends State<Matrix>
_setUpToMServer(null);
} else {
_setUpToMServer(toMConfigurations.tomServerInformation);
_setupAuthUrl(url: toMConfigurations.authUrl);
}
} catch (e) {
_setUpToMServer(null);
Expand Down Expand Up @@ -731,10 +738,10 @@ class MatrixState extends State<Matrix>
Future<void> _storePersistActiveAccount(Client newClient) async {
if (newClient.userID == null) return;
try {
Logs().e(
Logs().d(
'Matrix::_storePersistActiveAccount: clientName - ${newClient.clientName}',
);
Logs().e(
Logs().d(
'Matrix::_storePersistActiveAccount: userId - ${newClient.userID}',
);
final MultipleAccountRepository multipleAccountRepository =
Expand All @@ -757,6 +764,24 @@ class MatrixState extends State<Matrix>
didChangeAppLifecycleState(AppLifecycleState.paused);
}

Future<void> _setupAuthUrl({
String? url,
}) async {
final newAuthUrl = loginHomeserverSummary?.discoveryInformation
?.additionalProperties["m.authentication"]?["issuer"];
if (url != null) {
Logs().e(
'Matrix::_setupAuthUrl: newAuthUrl - $url',
);
_authUrl = url;
} else {
Logs().e(
'Matrix::_setupAuthUrl: newAuthUrl - $newAuthUrl',
);
_authUrl = newAuthUrl is String ? newAuthUrl : url;
}
}

@override
void didChangeAppLifecycleState(AppLifecycleState state) {
Logs().i('didChangeAppLifecycleState: AppLifecycleState = $state');
Expand Down

0 comments on commit 39f8d40

Please sign in to comment.