import 'package:flutter/foundation.dart'; import '../../../../core/logging/logger.dart'; import '../../data/repositories/auth_repository.dart'; import 'auth_state.dart'; class AuthBloc extends ChangeNotifier { AuthBloc({required AuthRepository repository}) : _repository = repository; final AuthRepository _repository; final Logger _logger = getLogger('features.auth.bloc'); AuthState _state = AuthState.initial; bool _handlingUnauthorized = false; AuthState get state => _state; Future start() async { _state = _state.copyWith(status: AuthStatus.loading, errorMessage: null); notifyListeners(); try { final user = await _repository.recoverSession(); if (user == null) { _state = const AuthState(status: AuthStatus.unauthenticated); } else { _state = AuthState(status: AuthStatus.authenticated, user: user); } notifyListeners(); } catch (error, stackTrace) { _logger.error( message: 'Session recovery failed: ${error.runtimeType}', error: error.runtimeType.toString(), stackTrace: stackTrace, ); await _repository.clearLocalSession(); _state = AuthState( status: AuthStatus.unauthenticated, errorMessage: _toSafeMessage(error), ); notifyListeners(); } } Future sendOtp(String email) async { await _repository.sendOtp(email); } Future loginWithOtp({ required String email, required String otp, }) async { final user = await _repository.loginWithEmailOtp(email: email, otp: otp); _logger.info(message: 'User logged in', extra: {'user_id': user.id}); _state = AuthState(status: AuthStatus.authenticated, user: user); notifyListeners(); } Future logout() async { try { await _repository.logout(); } catch (error, stackTrace) { _logger.error( message: 'User logout failed: ${error.runtimeType}', error: error.runtimeType.toString(), stackTrace: stackTrace, ); } _logger.info(message: 'User logged out'); _state = const AuthState(status: AuthStatus.unauthenticated); notifyListeners(); } Future handleUnauthorized401() async { if (_handlingUnauthorized) { return; } _handlingUnauthorized = true; try { await _repository.clearLocalSession(); _logger.warning(message: 'Session invalidated by 401 callback'); _state = const AuthState(status: AuthStatus.unauthenticated); notifyListeners(); } finally { _handlingUnauthorized = false; } } String _toSafeMessage(Object error) { return 'Request failed, please try again'; } }