feat: 实现 Auth 全局状态机与 401 统一处理机制
- 新增 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 失效路径
This commit is contained in:
@@ -11,6 +11,7 @@ abstract class AuthRepository {
|
||||
Future<AuthResponse> createSession(LoginRequest request);
|
||||
Future<AuthResponse> refreshSession(String refreshToken);
|
||||
Future<void> deleteSession();
|
||||
Future<void> clearSessionLocalOnly();
|
||||
Future<String?> getAccessToken();
|
||||
Future<String?> getRefreshToken();
|
||||
Future<bool> isAuthenticated();
|
||||
|
||||
@@ -64,9 +64,6 @@ class AuthRepositoryImpl implements AuthRepository {
|
||||
|
||||
@override
|
||||
Future<void> deleteSession() async {
|
||||
if (_onLogout != null) {
|
||||
await _onLogout!();
|
||||
}
|
||||
final refreshToken = await _tokenStorage.getRefreshToken();
|
||||
if (refreshToken != null) {
|
||||
try {
|
||||
@@ -75,6 +72,14 @@ class AuthRepositoryImpl implements AuthRepository {
|
||||
// ignore API errors during logout
|
||||
}
|
||||
}
|
||||
await clearSessionLocalOnly();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> clearSessionLocalOnly() async {
|
||||
if (_onLogout != null) {
|
||||
await _onLogout();
|
||||
}
|
||||
await _tokenStorage.clear();
|
||||
}
|
||||
|
||||
|
||||
@@ -10,13 +10,14 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
|
||||
on<AuthStarted>(_onStarted);
|
||||
on<AuthLoggedIn>(_onLoggedIn);
|
||||
on<AuthLoggedOut>(_onLoggedOut);
|
||||
on<AuthSessionInvalidated>(_onSessionInvalidated);
|
||||
}
|
||||
|
||||
Future<void> _onStarted(AuthStarted event, Emitter<AuthState> emit) async {
|
||||
emit(AuthLoading());
|
||||
final refreshToken = await _repository.getRefreshToken();
|
||||
if (refreshToken != null) {
|
||||
try {
|
||||
try {
|
||||
final refreshToken = await _repository.getRefreshToken();
|
||||
if (refreshToken != null) {
|
||||
final response = await _repository.refreshSession(refreshToken);
|
||||
emit(
|
||||
AuthAuthenticated(
|
||||
@@ -24,11 +25,23 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
emit(
|
||||
const AuthUnauthenticated(reason: AuthUnauthenticatedReason.signedOut),
|
||||
);
|
||||
} catch (_) {
|
||||
try {
|
||||
await _repository.clearSessionLocalOnly();
|
||||
} catch (_) {
|
||||
await _repository.deleteSession();
|
||||
// Keep state convergence even when storage cleanup fails.
|
||||
} finally {
|
||||
emit(
|
||||
const AuthUnauthenticated(
|
||||
reason: AuthUnauthenticatedReason.startupRecoveryFailed,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
emit(AuthUnauthenticated());
|
||||
}
|
||||
|
||||
void _onLoggedIn(AuthLoggedIn event, Emitter<AuthState> emit) {
|
||||
@@ -39,7 +52,29 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
|
||||
AuthLoggedOut event,
|
||||
Emitter<AuthState> emit,
|
||||
) async {
|
||||
await _repository.deleteSession();
|
||||
emit(AuthUnauthenticated());
|
||||
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),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
import '../../data/models/auth_response.dart';
|
||||
|
||||
enum AuthInvalidationSource { unauthorized401 }
|
||||
|
||||
abstract class AuthEvent extends Equatable {
|
||||
const AuthEvent();
|
||||
|
||||
@@ -20,3 +22,12 @@ class AuthLoggedIn extends AuthEvent {
|
||||
}
|
||||
|
||||
class AuthLoggedOut extends AuthEvent {}
|
||||
|
||||
class AuthSessionInvalidated extends AuthEvent {
|
||||
final AuthInvalidationSource source;
|
||||
|
||||
const AuthSessionInvalidated({required this.source});
|
||||
|
||||
@override
|
||||
List<Object?> get props => [source];
|
||||
}
|
||||
|
||||
@@ -14,6 +14,8 @@ class AuthInitial extends AuthState {}
|
||||
|
||||
class AuthLoading extends AuthState {}
|
||||
|
||||
enum AuthUnauthenticatedReason { signedOut, expired, startupRecoveryFailed }
|
||||
|
||||
class AuthAuthenticated extends AuthState {
|
||||
final AuthUser user;
|
||||
|
||||
@@ -23,4 +25,13 @@ class AuthAuthenticated extends AuthState {
|
||||
List<Object?> get props => [user];
|
||||
}
|
||||
|
||||
class AuthUnauthenticated extends AuthState {}
|
||||
class AuthUnauthenticated extends AuthState {
|
||||
final AuthUnauthenticatedReason reason;
|
||||
|
||||
const AuthUnauthenticated({
|
||||
this.reason = AuthUnauthenticatedReason.signedOut,
|
||||
});
|
||||
|
||||
@override
|
||||
List<Object?> get props => [reason];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user