Refactor signup page to use BLoC pattern and implement authentication repository
- Updated signup.dart to replace Provider with BLoC for state management. - Created AuthRepository to handle authentication logic and Firestore user management. - Added TripRepository and UserRepository for trip and user data management. - Implemented methods for user sign-in, sign-up, and data retrieval in repositories. - Enhanced trip management with create, update, delete, and participant management functionalities. - Updated AuthService to include new methods for sign-in and sign-up. - Removed unnecessary print statements from TripService for cleaner code. - Added dependencies for flutter_bloc and equatable in pubspec.yaml. Not tested yet
This commit is contained in:
150
lib/blocs/auth/auth_bloc.dart
Normal file
150
lib/blocs/auth/auth_bloc.dart
Normal file
@@ -0,0 +1,150 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import '../../repositories/auth_repository.dart';
|
||||
import 'auth_event.dart';
|
||||
import 'auth_state.dart';
|
||||
|
||||
class AuthBloc extends Bloc<AuthEvent, AuthState> {
|
||||
final AuthRepository _authRepository;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
Future<void> _onAuthCheckRequested(
|
||||
AuthCheckRequested event,
|
||||
Emitter<AuthState> emit,
|
||||
) async {
|
||||
emit(AuthLoading());
|
||||
|
||||
try {
|
||||
final currentUser = _authRepository.currentUser;
|
||||
|
||||
if (currentUser != null) {
|
||||
// Récupérer les données utilisateur depuis 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()));
|
||||
}
|
||||
}
|
||||
|
||||
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: 'Email ou mot de passe incorrect'));
|
||||
}
|
||||
} catch (e) {
|
||||
emit(AuthError(message: e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
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: 'Erreur lors de l\'inscription'));
|
||||
}
|
||||
} catch (e) {
|
||||
emit(AuthError(message: e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
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: 'Connexion Google annulée'));
|
||||
}
|
||||
} catch (e) {
|
||||
emit(AuthError(message: e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
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: 'Connexion Apple annulée'));
|
||||
}
|
||||
} catch (e) {
|
||||
emit(AuthError(message: e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _onSignOutRequested(
|
||||
AuthSignOutRequested event,
|
||||
Emitter<AuthState> emit,
|
||||
) async {
|
||||
await _authRepository.signOut();
|
||||
emit(AuthUnauthenticated());
|
||||
}
|
||||
|
||||
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()));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user