From e19bdb8728623da1d0902e63745e8330ad7dbaf2 Mon Sep 17 00:00:00 2001 From: selfrelease Date: Sat, 18 Jul 2026 20:04:00 +0800 Subject: [PATCH] fix: harden iOS authentication recovery --- .../core/auth/auth_session_controller.dart | 20 +++++++++++++++---- mobile/lib/core/auth/login_page.dart | 2 +- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/mobile/lib/core/auth/auth_session_controller.dart b/mobile/lib/core/auth/auth_session_controller.dart index adf9a81..3d1d165 100644 --- a/mobile/lib/core/auth/auth_session_controller.dart +++ b/mobile/lib/core/auth/auth_session_controller.dart @@ -19,11 +19,19 @@ class AuthSessionController extends AsyncNotifier { static const _appAuth = FlutterAppAuth(); @override - Future build() => _restore(); + Future build() async { + try { + return await _restore(); + } catch (_) { + // 安全存储在模拟器重装、系统升级或钥匙串状态变化后可能暂时不可读。 + // 将其视为无本地会话,避免阻断用户重新登录。 + return null; + } + } Future login() async { state = const AsyncLoading(); - state = await AsyncValue.guard(() async { + try { final result = await _appAuth.authorizeAndExchangeCode( AuthorizationTokenRequest( oidcClientId, @@ -34,8 +42,12 @@ class AuthSessionController extends AsyncNotifier { allowInsecureConnections: oidcIssuer.startsWith('http://'), ), ); - return _storeResponse(result); - }); + state = AsyncData(await _storeResponse(result)); + } on FlutterAppAuthUserCancelledException { + state = const AsyncData(null); + } catch (error, stackTrace) { + state = AsyncError(error, stackTrace); + } } Future logout() async { diff --git a/mobile/lib/core/auth/login_page.dart b/mobile/lib/core/auth/login_page.dart index e4def83..7562015 100644 --- a/mobile/lib/core/auth/login_page.dart +++ b/mobile/lib/core/auth/login_page.dart @@ -39,7 +39,7 @@ class LoginPage extends ConsumerWidget { if (auth.hasError) ...[ const SizedBox(height: 16), Text( - '登录失败:${auth.error}', + '登录失败,请检查网络后重试', textAlign: TextAlign.center, style: TextStyle( color: Theme.of(context).colorScheme.error,