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()));
|
||||
}
|
||||
}
|
||||
}
|
||||
55
lib/blocs/auth/auth_event.dart
Normal file
55
lib/blocs/auth/auth_event.dart
Normal file
@@ -0,0 +1,55 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
|
||||
abstract class AuthEvent extends Equatable {
|
||||
const AuthEvent();
|
||||
|
||||
@override
|
||||
List<Object?> get props => [];
|
||||
}
|
||||
|
||||
class AuthCheckRequested extends AuthEvent {}
|
||||
|
||||
class AuthSignInRequested extends AuthEvent {
|
||||
final String email;
|
||||
final String password;
|
||||
|
||||
const AuthSignInRequested({
|
||||
required this.email,
|
||||
required this.password,
|
||||
});
|
||||
|
||||
@override
|
||||
List<Object?> get props => [email, password];
|
||||
}
|
||||
|
||||
class AuthSignUpRequested extends AuthEvent {
|
||||
final String email;
|
||||
final String password;
|
||||
final String nom;
|
||||
final String prenom;
|
||||
|
||||
const AuthSignUpRequested({
|
||||
required this.email,
|
||||
required this.password,
|
||||
required this.nom,
|
||||
required this.prenom,
|
||||
});
|
||||
|
||||
@override
|
||||
List<Object?> get props => [email, password, nom, prenom];
|
||||
}
|
||||
|
||||
class AuthGoogleSignInRequested extends AuthEvent {}
|
||||
|
||||
class AuthAppleSignInRequested extends AuthEvent {}
|
||||
|
||||
class AuthSignOutRequested extends AuthEvent {}
|
||||
|
||||
class AuthPasswordResetRequested extends AuthEvent {
|
||||
final String email;
|
||||
|
||||
const AuthPasswordResetRequested({required this.email});
|
||||
|
||||
@override
|
||||
List<Object?> get props => [email];
|
||||
}
|
||||
42
lib/blocs/auth/auth_state.dart
Normal file
42
lib/blocs/auth/auth_state.dart
Normal file
@@ -0,0 +1,42 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
import '../../data/models/user.dart';
|
||||
|
||||
abstract class AuthState extends Equatable {
|
||||
const AuthState();
|
||||
|
||||
@override
|
||||
List<Object?> get props => [];
|
||||
}
|
||||
|
||||
class AuthInitial extends AuthState {}
|
||||
|
||||
class AuthLoading extends AuthState {}
|
||||
|
||||
class AuthAuthenticated extends AuthState {
|
||||
final User user;
|
||||
|
||||
const AuthAuthenticated({required this.user});
|
||||
|
||||
@override
|
||||
List<Object?> get props => [user];
|
||||
}
|
||||
|
||||
class AuthUnauthenticated extends AuthState {}
|
||||
|
||||
class AuthError extends AuthState {
|
||||
final String message;
|
||||
|
||||
const AuthError({required this.message});
|
||||
|
||||
@override
|
||||
List<Object?> get props => [message];
|
||||
}
|
||||
|
||||
class AuthPasswordResetSent extends AuthState {
|
||||
final String email;
|
||||
|
||||
const AuthPasswordResetSent({required this.email});
|
||||
|
||||
@override
|
||||
List<Object?> get props => [email];
|
||||
}
|
||||
Reference in New Issue
Block a user