b34697660d
- 新增 AuthSessionInvalidated 事件处理 token 失效场景 - ApiInterceptor 新增 authFailureCallback 单飞机制 - AuthBloc 区分 manual logout 与 auto expiry 语义 - 新增 startup recovery fallback 防止启动卡死 feat: 重构 Calendar DayWeek 视图事件布局引擎 - 新增 DayEventLayoutEngine 解耦事件计算与渲染 - 新增 DayTimelineMetrics 统一时间轴常量 - 新增 DayViewScale 支持捏合缩放 feat: 新增 Settings 页面共享 UI 组件 - 新增 BackTitlePageHeader 统一页面 header - 新增 DetailHeaderActionMenu 统一操作菜单 - 新增 DestructiveActionSheet 统一删除确认 - 新增 AppToggleSwitch 统一开关组件 feat: Chat UI Schema 支持导航操作 - 支持 navigation 类型 action 触发内部路由跳转 - 新增路径验证与参数处理 chore: 更新相关测试覆盖 auth 失效路径
81 lines
2.2 KiB
Dart
81 lines
2.2 KiB
Dart
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import '../../data/auth_repository.dart';
|
|
import 'auth_event.dart';
|
|
import 'auth_state.dart';
|
|
|
|
class AuthBloc extends Bloc<AuthEvent, AuthState> {
|
|
final AuthRepository _repository;
|
|
|
|
AuthBloc(this._repository) : super(AuthInitial()) {
|
|
on<AuthStarted>(_onStarted);
|
|
on<AuthLoggedIn>(_onLoggedIn);
|
|
on<AuthLoggedOut>(_onLoggedOut);
|
|
on<AuthSessionInvalidated>(_onSessionInvalidated);
|
|
}
|
|
|
|
Future<void> _onStarted(AuthStarted event, Emitter<AuthState> emit) async {
|
|
emit(AuthLoading());
|
|
try {
|
|
final refreshToken = await _repository.getRefreshToken();
|
|
if (refreshToken != null) {
|
|
final response = await _repository.refreshSession(refreshToken);
|
|
emit(
|
|
AuthAuthenticated(
|
|
user: AuthUser(id: response.user.id, email: response.user.email),
|
|
),
|
|
);
|
|
return;
|
|
}
|
|
emit(
|
|
const AuthUnauthenticated(reason: AuthUnauthenticatedReason.signedOut),
|
|
);
|
|
} catch (_) {
|
|
try {
|
|
await _repository.clearSessionLocalOnly();
|
|
} catch (_) {
|
|
// Keep state convergence even when storage cleanup fails.
|
|
} finally {
|
|
emit(
|
|
const AuthUnauthenticated(
|
|
reason: AuthUnauthenticatedReason.startupRecoveryFailed,
|
|
),
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
void _onLoggedIn(AuthLoggedIn event, Emitter<AuthState> emit) {
|
|
emit(AuthAuthenticated(user: event.user));
|
|
}
|
|
|
|
Future<void> _onLoggedOut(
|
|
AuthLoggedOut event,
|
|
Emitter<AuthState> emit,
|
|
) async {
|
|
try {
|
|
await _repository.deleteSession();
|
|
} catch (_) {
|
|
// Keep state convergence even when logout cleanup fails.
|
|
} finally {
|
|
emit(
|
|
const AuthUnauthenticated(reason: AuthUnauthenticatedReason.signedOut),
|
|
);
|
|
}
|
|
}
|
|
|
|
Future<void> _onSessionInvalidated(
|
|
AuthSessionInvalidated event,
|
|
Emitter<AuthState> emit,
|
|
) async {
|
|
try {
|
|
await _repository.clearSessionLocalOnly();
|
|
} catch (_) {
|
|
// Keep state convergence even when local cleanup fails.
|
|
} finally {
|
|
emit(
|
|
const AuthUnauthenticated(reason: AuthUnauthenticatedReason.expired),
|
|
);
|
|
}
|
|
}
|
|
}
|