- Updated Group, Trip, User, and other model classes to include comprehensive documentation for better understanding and maintainability. - Improved error handling and logging in services, including AuthService, ErrorService, and StorageService. - Added validation and business logic explanations in ExpenseService and TripService. - Refactored method comments to follow a consistent format across the codebase. - Translated error messages and comments from French to English for consistency.
204 lines
6.3 KiB
Dart
204 lines
6.3 KiB
Dart
/// Business Logic Component for managing authentication state.
|
|
///
|
|
/// The [AuthBloc] handles authentication-related events and manages the
|
|
/// authentication state throughout the application. It coordinates with
|
|
/// the [AuthRepository] to perform authentication operations and emits
|
|
/// appropriate states based on the results.
|
|
///
|
|
/// Supported authentication methods:
|
|
/// - Email and password authentication
|
|
/// - Google Sign-In
|
|
/// - Apple Sign-In
|
|
/// - Password reset functionality
|
|
///
|
|
/// This bloc handles the following events:
|
|
/// - [AuthCheckRequested]: Verifies current authentication status
|
|
/// - [AuthSignInRequested]: Processes email/password sign-in
|
|
/// - [AuthSignUpRequested]: Processes user registration
|
|
/// - [AuthGoogleSignInRequested]: Processes Google authentication
|
|
/// - [AuthAppleSignInRequested]: Processes Apple authentication
|
|
/// - [AuthSignOutRequested]: Processes user sign-out
|
|
/// - [AuthPasswordResetRequested]: Processes password reset requests
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import '../../repositories/auth_repository.dart';
|
|
import 'auth_event.dart';
|
|
import 'auth_state.dart';
|
|
|
|
/// BLoC for managing authentication state and operations.
|
|
class AuthBloc extends Bloc<AuthEvent, AuthState> {
|
|
/// Repository for authentication operations.
|
|
final AuthRepository _authRepository;
|
|
|
|
/// Creates an [AuthBloc] with the provided [authRepository].
|
|
///
|
|
/// The bloc starts in the [AuthInitial] state and registers event handlers
|
|
/// for all supported authentication events.
|
|
AuthBloc({required AuthRepository authRepository})
|
|
: _authRepository = authRepository,
|
|
super(AuthInitial()) {
|
|
on<AuthCheckRequested>(_onAuthCheckRequested);
|
|
on<AuthSignInRequested>(_onSignInRequested);
|
|
on<AuthSignUpRequested>(_onSignUpRequested);
|
|
on<AuthGoogleSignInRequested>(_onGoogleSignInRequested);
|
|
on<AuthAppleSignInRequested>(_onAppleSignInRequested);
|
|
on<AuthSignOutRequested>(_onSignOutRequested);
|
|
on<AuthPasswordResetRequested>(_onPasswordResetRequested);
|
|
}
|
|
|
|
/// Handles [AuthCheckRequested] events.
|
|
///
|
|
/// Checks if a user is currently authenticated and emits the appropriate state.
|
|
/// If a user is found, attempts to fetch user data from Firestore.
|
|
Future<void> _onAuthCheckRequested(
|
|
AuthCheckRequested event,
|
|
Emitter<AuthState> emit,
|
|
) async {
|
|
emit(AuthLoading());
|
|
|
|
try {
|
|
final currentUser = _authRepository.currentUser;
|
|
|
|
if (currentUser != null) {
|
|
// Fetch user data from Firestore
|
|
final user = await _authRepository.getUserFromFirestore(currentUser.uid);
|
|
|
|
if (user != null) {
|
|
emit(AuthAuthenticated(user: user));
|
|
} else {
|
|
emit(AuthUnauthenticated());
|
|
}
|
|
} else {
|
|
emit(AuthUnauthenticated());
|
|
}
|
|
} catch (e) {
|
|
emit(AuthError(message: e.toString()));
|
|
}
|
|
}
|
|
|
|
/// Handles [AuthSignInRequested] events.
|
|
///
|
|
/// Attempts to sign in a user with the provided email and password.
|
|
/// Emits [AuthAuthenticated] on success or [AuthError] on failure.
|
|
Future<void> _onSignInRequested(
|
|
AuthSignInRequested event,
|
|
Emitter<AuthState> emit,
|
|
) async {
|
|
emit(AuthLoading());
|
|
|
|
try {
|
|
final user = await _authRepository.signInWithEmailAndPassword(
|
|
email: event.email,
|
|
password: event.password,
|
|
);
|
|
|
|
if (user != null) {
|
|
emit(AuthAuthenticated(user: user));
|
|
} else {
|
|
emit(const AuthError(message: 'Invalid email or password'));
|
|
}
|
|
} catch (e) {
|
|
emit(AuthError(message: e.toString()));
|
|
}
|
|
}
|
|
|
|
/// Handles [AuthSignUpRequested] events.
|
|
///
|
|
/// Attempts to create a new user account with the provided information.
|
|
/// Emits [AuthAuthenticated] on success or [AuthError] on failure.
|
|
Future<void> _onSignUpRequested(
|
|
AuthSignUpRequested event,
|
|
Emitter<AuthState> emit,
|
|
) async {
|
|
emit(AuthLoading());
|
|
|
|
try {
|
|
final user = await _authRepository.signUpWithEmailAndPassword(
|
|
email: event.email,
|
|
password: event.password,
|
|
nom: event.nom,
|
|
prenom: event.prenom,
|
|
);
|
|
|
|
if (user != null) {
|
|
emit(AuthAuthenticated(user: user));
|
|
} else {
|
|
emit(const AuthError(message: 'Registration failed'));
|
|
}
|
|
} catch (e) {
|
|
emit(AuthError(message: e.toString()));
|
|
}
|
|
}
|
|
|
|
/// Handles [AuthGoogleSignInRequested] events.
|
|
///
|
|
/// Attempts to sign in the user using Google authentication.
|
|
/// Emits [AuthAuthenticated] on success or [AuthError] on failure.
|
|
Future<void> _onGoogleSignInRequested(
|
|
AuthGoogleSignInRequested event,
|
|
Emitter<AuthState> emit,
|
|
) async {
|
|
emit(AuthLoading());
|
|
|
|
try {
|
|
final user = await _authRepository.signInWithGoogle();
|
|
|
|
if (user != null) {
|
|
emit(AuthAuthenticated(user: user));
|
|
} else {
|
|
emit(const AuthError(message: 'Google sign-in cancelled'));
|
|
}
|
|
} catch (e) {
|
|
emit(AuthError(message: e.toString()));
|
|
}
|
|
}
|
|
|
|
/// Handles [AuthAppleSignInRequested] events.
|
|
///
|
|
/// Attempts to sign in the user using Apple authentication.
|
|
/// Emits [AuthAuthenticated] on success or [AuthError] on failure.
|
|
Future<void> _onAppleSignInRequested(
|
|
AuthAppleSignInRequested event,
|
|
Emitter<AuthState> emit,
|
|
) async {
|
|
emit(AuthLoading());
|
|
|
|
try {
|
|
final user = await _authRepository.signInWithApple();
|
|
|
|
if (user != null) {
|
|
emit(AuthAuthenticated(user: user));
|
|
} else {
|
|
emit(const AuthError(message: 'Apple sign-in cancelled'));
|
|
}
|
|
} catch (e) {
|
|
emit(AuthError(message: e.toString()));
|
|
}
|
|
}
|
|
|
|
/// Handles [AuthSignOutRequested] events.
|
|
///
|
|
/// Signs out the current user and emits [AuthUnauthenticated].
|
|
Future<void> _onSignOutRequested(
|
|
AuthSignOutRequested event,
|
|
Emitter<AuthState> emit,
|
|
) async {
|
|
await _authRepository.signOut();
|
|
emit(AuthUnauthenticated());
|
|
}
|
|
|
|
/// Handles [AuthPasswordResetRequested] events.
|
|
///
|
|
/// Sends a password reset email to the specified email address.
|
|
/// Emits [AuthPasswordResetSent] on success or [AuthError] on failure.
|
|
Future<void> _onPasswordResetRequested(
|
|
AuthPasswordResetRequested event,
|
|
Emitter<AuthState> emit,
|
|
) async {
|
|
try {
|
|
await _authRepository.resetPassword(event.email);
|
|
emit(AuthPasswordResetSent(email: event.email));
|
|
} catch (e) {
|
|
emit(AuthError(message: e.toString()));
|
|
}
|
|
}
|
|
} |