fix: harden iOS authentication recovery

This commit is contained in:
selfrelease
2026-07-18 20:04:00 +08:00
parent b311be6aa6
commit e19bdb8728
2 changed files with 17 additions and 5 deletions
@@ -19,11 +19,19 @@ class AuthSessionController extends AsyncNotifier<AuthSession?> {
static const _appAuth = FlutterAppAuth();
@override
Future<AuthSession?> build() => _restore();
Future<AuthSession?> build() async {
try {
return await _restore();
} catch (_) {
// 安全存储在模拟器重装、系统升级或钥匙串状态变化后可能暂时不可读。
// 将其视为无本地会话,避免阻断用户重新登录。
return null;
}
}
Future<void> 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<AuthSession?> {
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<void> logout() async {
+1 -1
View File
@@ -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,