fix: harden iOS authentication recovery
This commit is contained in:
@@ -19,11 +19,19 @@ class AuthSessionController extends AsyncNotifier<AuthSession?> {
|
|||||||
static const _appAuth = FlutterAppAuth();
|
static const _appAuth = FlutterAppAuth();
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<AuthSession?> build() => _restore();
|
Future<AuthSession?> build() async {
|
||||||
|
try {
|
||||||
|
return await _restore();
|
||||||
|
} catch (_) {
|
||||||
|
// 安全存储在模拟器重装、系统升级或钥匙串状态变化后可能暂时不可读。
|
||||||
|
// 将其视为无本地会话,避免阻断用户重新登录。
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Future<void> login() async {
|
Future<void> login() async {
|
||||||
state = const AsyncLoading();
|
state = const AsyncLoading();
|
||||||
state = await AsyncValue.guard(() async {
|
try {
|
||||||
final result = await _appAuth.authorizeAndExchangeCode(
|
final result = await _appAuth.authorizeAndExchangeCode(
|
||||||
AuthorizationTokenRequest(
|
AuthorizationTokenRequest(
|
||||||
oidcClientId,
|
oidcClientId,
|
||||||
@@ -34,8 +42,12 @@ class AuthSessionController extends AsyncNotifier<AuthSession?> {
|
|||||||
allowInsecureConnections: oidcIssuer.startsWith('http://'),
|
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 {
|
Future<void> logout() async {
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ class LoginPage extends ConsumerWidget {
|
|||||||
if (auth.hasError) ...[
|
if (auth.hasError) ...[
|
||||||
const SizedBox(height: 16),
|
const SizedBox(height: 16),
|
||||||
Text(
|
Text(
|
||||||
'登录失败:${auth.error}',
|
'登录失败,请检查网络后重试',
|
||||||
textAlign: TextAlign.center,
|
textAlign: TextAlign.center,
|
||||||
style: TextStyle(
|
style: TextStyle(
|
||||||
color: Theme.of(context).colorScheme.error,
|
color: Theme.of(context).colorScheme.error,
|
||||||
|
|||||||
Reference in New Issue
Block a user