From 2aaf941dda14dbc49141e055e5ec8bf10e457929 Mon Sep 17 00:00:00 2001 From: shenlong <139912620+shenlong-tanwen@users.noreply.github.com> Date: Thu, 4 Jan 2024 20:44:40 +0000 Subject: [PATCH] refactor(mobile): use startOAuth and server features flags (#6155) Co-authored-by: shenlong-tanwen <139912620+shalong-tanwen@users.noreply.github.com> --- .../modules/login/services/oauth.service.dart | 5 +-- mobile/lib/modules/login/ui/login_form.dart | 34 +++++++++---------- .../server_info/server_config.model.dart | 16 ++++++--- .../server_info/server_features.model.dart | 26 +++++++++++--- .../providers/server_info.provider.dart | 11 +++--- 5 files changed, 59 insertions(+), 33 deletions(-) diff --git a/mobile/lib/modules/login/services/oauth.service.dart b/mobile/lib/modules/login/services/oauth.service.dart index e7fd433446..8f34c968eb 100644 --- a/mobile/lib/modules/login/services/oauth.service.dart +++ b/mobile/lib/modules/login/services/oauth.service.dart @@ -11,15 +11,16 @@ class OAuthService { final log = Logger('OAuthService'); OAuthService(this._apiService); - Future getOAuthServerConfig( + Future getOAuthServerUrl( String serverUrl, ) async { // Resolve API server endpoint from user provided serverUrl await _apiService.resolveAndSetEndpoint(serverUrl); - return await _apiService.oAuthApi.generateOAuthConfig( + final dto = await _apiService.oAuthApi.startOAuth( OAuthConfigDto(redirectUri: '$callbackUrlScheme:/'), ); + return dto?.url; } Future oAuthLogin(String oauthUrl) async { diff --git a/mobile/lib/modules/login/ui/login_form.dart b/mobile/lib/modules/login/ui/login_form.dart index 6c12742e4c..1c5a6db0fb 100644 --- a/mobile/lib/modules/login/ui/login_form.dart +++ b/mobile/lib/modules/login/ui/login_form.dart @@ -12,6 +12,7 @@ import 'package:immich_mobile/shared/providers/api.provider.dart'; import 'package:immich_mobile/shared/providers/asset.provider.dart'; import 'package:immich_mobile/modules/login/providers/authentication.provider.dart'; import 'package:immich_mobile/modules/backup/providers/backup.provider.dart'; +import 'package:immich_mobile/shared/providers/server_info.provider.dart'; import 'package:immich_mobile/shared/ui/immich_logo.dart'; import 'package:immich_mobile/shared/ui/immich_title_text.dart'; import 'package:immich_mobile/shared/ui/immich_toast.dart'; @@ -65,18 +66,18 @@ class LoginForm extends HookConsumerWidget { isLoadingServer.value = true; final endpoint = await apiService.resolveAndSetEndpoint(serverUrl); - final loginConfig = await apiService.oAuthApi.generateOAuthConfig( - OAuthConfigDto(redirectUri: serverUrl), - ); + // Fetch and load server config and features + await ref.read(serverInfoProvider.notifier).getServerInfo(); - if (loginConfig != null) { - isOauthEnable.value = loginConfig.enabled; - isPasswordLoginEnable.value = loginConfig.passwordLoginEnabled; - oAuthButtonLabel.value = loginConfig.buttonText ?? 'OAuth'; - } else { - isOauthEnable.value = false; - isPasswordLoginEnable.value = true; - } + final serverInfo = ref.read(serverInfoProvider); + final features = serverInfo.serverFeatures; + final config = serverInfo.serverConfig; + + isOauthEnable.value = features.oauthEnabled; + isPasswordLoginEnable.value = features.passwordLogin; + oAuthButtonLabel.value = config.oauthButtonText.isNotEmpty + ? config.oauthButtonText + : 'OAuth'; serverEndpoint.value = endpoint; } on ApiException catch (e) { @@ -183,11 +184,11 @@ class LoginForm extends HookConsumerWidget { oAuthLogin() async { var oAuthService = ref.watch(oAuthServiceProvider); ref.watch(assetProvider.notifier).clearAllAsset(); - OAuthConfigResponseDto? oAuthServerConfig; + String? oAuthServerUrl; try { - oAuthServerConfig = await oAuthService - .getOAuthServerConfig(sanitizeUrl(serverEndpointController.text)); + oAuthServerUrl = await oAuthService + .getOAuthServerUrl(sanitizeUrl(serverEndpointController.text)); isLoading.value = true; } catch (e) { @@ -200,9 +201,8 @@ class LoginForm extends HookConsumerWidget { return; } - if (oAuthServerConfig != null && oAuthServerConfig.enabled) { - var loginResponseDto = - await oAuthService.oAuthLogin(oAuthServerConfig.url!); + if (oAuthServerUrl != null) { + var loginResponseDto = await oAuthService.oAuthLogin(oAuthServerUrl); if (loginResponseDto != null) { var isSuccess = await ref diff --git a/mobile/lib/shared/models/server_info/server_config.model.dart b/mobile/lib/shared/models/server_info/server_config.model.dart index 7833301cc6..8936939135 100644 --- a/mobile/lib/shared/models/server_info/server_config.model.dart +++ b/mobile/lib/shared/models/server_info/server_config.model.dart @@ -2,40 +2,46 @@ import 'package:openapi/api.dart'; class ServerConfig { final int trashDays; + final String oauthButtonText; final String externalDomain; const ServerConfig({ required this.trashDays, + required this.oauthButtonText, required this.externalDomain, }); ServerConfig copyWith({ int? trashDays, + String? oauthButtonText, String? externalDomain, }) { return ServerConfig( trashDays: trashDays ?? this.trashDays, + oauthButtonText: oauthButtonText ?? this.oauthButtonText, externalDomain: externalDomain ?? this.externalDomain, ); } @override String toString() => - 'ServerConfig(trashDays: $trashDays, externalDomain: $externalDomain)'; + 'ServerConfig(trashDays: $trashDays, oauthButtonText: $oauthButtonText, externalDomain: $externalDomain)'; ServerConfig.fromDto(ServerConfigDto dto) : trashDays = dto.trashDays, + oauthButtonText = dto.oauthButtonText, externalDomain = dto.externalDomain; @override - bool operator ==(Object other) { + bool operator ==(covariant ServerConfig other) { if (identical(this, other)) return true; - return other is ServerConfig && - other.trashDays == trashDays && + return other.trashDays == trashDays && + other.oauthButtonText == oauthButtonText && other.externalDomain == externalDomain; } @override - int get hashCode => trashDays.hashCode ^ externalDomain.hashCode; + int get hashCode => + trashDays.hashCode ^ oauthButtonText.hashCode ^ externalDomain.hashCode; } diff --git a/mobile/lib/shared/models/server_info/server_features.model.dart b/mobile/lib/shared/models/server_info/server_features.model.dart index acf4c3d2da..fee88869ed 100644 --- a/mobile/lib/shared/models/server_info/server_features.model.dart +++ b/mobile/lib/shared/models/server_info/server_features.model.dart @@ -3,40 +3,56 @@ import 'package:openapi/api.dart'; class ServerFeatures { final bool trash; final bool map; + final bool oauthEnabled; + final bool passwordLogin; const ServerFeatures({ required this.trash, required this.map, + required this.oauthEnabled, + required this.passwordLogin, }); ServerFeatures copyWith({ bool? trash, bool? map, + bool? oauthEnabled, + bool? passwordLogin, }) { return ServerFeatures( trash: trash ?? this.trash, map: map ?? this.map, + oauthEnabled: oauthEnabled ?? this.oauthEnabled, + passwordLogin: passwordLogin ?? this.passwordLogin, ); } @override String toString() { - return 'ServerFeatures(trash: $trash, map: $map)'; + return 'ServerFeatures(trash: $trash, map: $map, oauthEnabled: $oauthEnabled, passwordLogin: $passwordLogin)'; } ServerFeatures.fromDto(ServerFeaturesDto dto) : trash = dto.trash, - map = dto.map; + map = dto.map, + oauthEnabled = dto.oauth, + passwordLogin = dto.passwordLogin; @override - bool operator ==(Object other) { + bool operator ==(covariant ServerFeatures other) { if (identical(this, other)) return true; - return other is ServerFeatures && other.trash == trash && other.map == map; + return other.trash == trash && + other.map == map && + other.oauthEnabled == oauthEnabled && + other.passwordLogin == passwordLogin; } @override int get hashCode { - return trash.hashCode ^ map.hashCode; + return trash.hashCode ^ + map.hashCode ^ + oauthEnabled.hashCode ^ + passwordLogin.hashCode; } } diff --git a/mobile/lib/shared/providers/server_info.provider.dart b/mobile/lib/shared/providers/server_info.provider.dart index 0f2d9d1c49..bebed7fc27 100644 --- a/mobile/lib/shared/providers/server_info.provider.dart +++ b/mobile/lib/shared/providers/server_info.provider.dart @@ -26,9 +26,12 @@ class ServerInfoNotifier extends StateNotifier { serverFeatures: const ServerFeatures( map: true, trash: true, + oauthEnabled: false, + passwordLogin: true, ), serverConfig: const ServerConfig( trashDays: 30, + oauthButtonText: '', externalDomain: '', ), serverDiskInfo: const ServerDiskInfo( @@ -45,10 +48,10 @@ class ServerInfoNotifier extends StateNotifier { final ServerInfoService _serverInfoService; - getServerInfo() { - getServerVersion(); - getServerFeatures(); - getServerConfig(); + Future getServerInfo() async { + await getServerVersion(); + await getServerFeatures(); + await getServerConfig(); } getServerVersion() async {