games_app/prediqo_mobile/lib/features/auth/bloc/auth_bloc.dart
2026-06-29 15:01:23 +02:00

89 lines
3.1 KiB
Dart

/// Authentication BLoC layer: login, registration, session check, logout, and 2FA setup.
library;
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:equatable/equatable.dart';
import '../../../data/repositories/auth_repository_impl.dart';
part 'auth_event.dart';
part 'auth_state.dart';
/// Manages authentication state and coordinates with [AuthRepositoryImpl].
class AuthBloc extends Bloc<AuthEvent, AuthState> {
/// Creates an [AuthBloc] backed by [authRepo].
AuthBloc(this._authRepo) : super(const AuthState.initial()) {
on<AuthCheckRequested>(_onCheckRequested);
on<AuthLoginRequested>(_onLogin);
on<AuthRegisterRequested>(_onRegister);
on<AuthLogoutRequested>(_onLogout);
on<Auth2FASetupRequested>(_on2FASetup);
on<Auth2FAEnableRequested>(_on2FAEnable);
}
final AuthRepositoryImpl _authRepo;
/// Restores session from stored credentials; emits [AuthStatus.authenticated] or [AuthStatus.unauthenticated].
Future<void> _onCheckRequested(AuthCheckRequested event, Emitter<AuthState> emit) async {
emit(const AuthState.loading());
try {
final loggedIn = await _authRepo.isLoggedIn();
if (loggedIn) {
final stored = await _authRepo.getStoredUser();
emit(AuthState.authenticated(userId: stored.userId, role: stored.role));
} else {
emit(const AuthState.unauthenticated());
}
} catch (_) {
emit(const AuthState.unauthenticated());
}
}
/// Signs in with email, password, and optional TOTP; navigates via listener on success.
Future<void> _onLogin(AuthLoginRequested event, Emitter<AuthState> emit) async {
emit(const AuthState.loading());
try {
final result = await _authRepo.login(event.email, event.password, totpCode: event.totpCode);
emit(AuthState.authenticated(userId: result.userId, role: result.role));
} catch (e) {
emit(AuthState.error(e.toString()));
}
}
/// Registers a new account; emits [AuthStatus.registered] on success.
Future<void> _onRegister(AuthRegisterRequested event, Emitter<AuthState> emit) async {
emit(const AuthState.loading());
try {
await _authRepo.register(event.email, event.password);
emit(const AuthState.registered());
} catch (e) {
emit(AuthState.error(e.toString()));
}
}
/// Clears stored session and emits [AuthStatus.unauthenticated].
Future<void> _onLogout(AuthLogoutRequested event, Emitter<AuthState> emit) async {
await _authRepo.logout();
emit(const AuthState.unauthenticated());
}
/// Fetches TOTP setup data (QR URL) from the API for enrollment.
Future<void> _on2FASetup(Auth2FASetupRequested event, Emitter<AuthState> emit) async {
try {
final data = await _authRepo.setup2FA();
emit(AuthState.totpSetup(data));
} catch (e) {
emit(AuthState.error(e.toString()));
}
}
/// Verifies and enables 2FA with the user-supplied TOTP code.
Future<void> _on2FAEnable(Auth2FAEnableRequested event, Emitter<AuthState> emit) async {
try {
await _authRepo.enable2FA(event.code);
emit(const AuthState.totpEnabled());
} catch (e) {
emit(AuthState.error(e.toString()));
}
}
}