fAdd phone number support to user authentication events and methods
This commit is contained in:
@@ -1,16 +1,16 @@
|
||||
/// 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
|
||||
@@ -20,6 +20,7 @@
|
||||
/// - [AuthSignOutRequested]: Processes user sign-out
|
||||
/// - [AuthPasswordResetRequested]: Processes password reset requests
|
||||
library;
|
||||
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import '../../repositories/auth_repository.dart';
|
||||
import 'auth_event.dart';
|
||||
@@ -31,23 +32,25 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
|
||||
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()) {
|
||||
: _authRepository = authRepository,
|
||||
super(AuthInitial()) {
|
||||
on<AuthCheckRequested>(_onAuthCheckRequested);
|
||||
on<AuthSignInRequested>(_onSignInRequested);
|
||||
on<AuthSignUpRequested>(_onSignUpRequested);
|
||||
on<AuthGoogleSignInRequested>(_onGoogleSignInRequested);
|
||||
on<AuthGoogleSignUpRequested>(_onGoogleSignUpRequested);
|
||||
on<AuthAppleSignInRequested>(_onAppleSignInRequested);
|
||||
on<AuthAppleSignUpRequested>(_onAppleSignUpRequested);
|
||||
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(
|
||||
@@ -55,14 +58,16 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
|
||||
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);
|
||||
|
||||
final user = await _authRepository.getUserFromFirestore(
|
||||
currentUser.uid,
|
||||
);
|
||||
|
||||
if (user != null) {
|
||||
emit(AuthAuthenticated(user: user));
|
||||
} else {
|
||||
@@ -77,7 +82,7 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
|
||||
}
|
||||
|
||||
/// 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(
|
||||
@@ -85,7 +90,7 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
|
||||
Emitter<AuthState> emit,
|
||||
) async {
|
||||
emit(AuthLoading());
|
||||
|
||||
|
||||
try {
|
||||
final user = await _authRepository.signInWithEmailAndPassword(
|
||||
email: event.email,
|
||||
@@ -103,7 +108,7 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
|
||||
}
|
||||
|
||||
/// 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(
|
||||
@@ -111,19 +116,20 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
|
||||
Emitter<AuthState> emit,
|
||||
) async {
|
||||
emit(AuthLoading());
|
||||
|
||||
|
||||
try {
|
||||
final user = await _authRepository.signUpWithEmailAndPassword(
|
||||
email: event.email,
|
||||
password: event.password,
|
||||
nom: event.nom,
|
||||
prenom: event.prenom,
|
||||
phoneNumber: event.phoneNumber,
|
||||
);
|
||||
|
||||
if (user != null) {
|
||||
emit(AuthAuthenticated(user: user));
|
||||
} else {
|
||||
emit(const AuthError(message: 'Registration failed'));
|
||||
emit(const AuthError(message: 'Failed to create account'));
|
||||
}
|
||||
} catch (e) {
|
||||
emit(AuthError(message: e.toString()));
|
||||
@@ -131,7 +137,7 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
|
||||
}
|
||||
|
||||
/// Handles [AuthGoogleSignInRequested] events.
|
||||
///
|
||||
///
|
||||
/// Attempts to sign in the user using Google authentication.
|
||||
/// Emits [AuthAuthenticated] on success or [AuthError] on failure.
|
||||
Future<void> _onGoogleSignInRequested(
|
||||
@@ -139,14 +145,57 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
|
||||
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'));
|
||||
emit(
|
||||
const AuthError(
|
||||
message:
|
||||
'Utilisateur n\'est pas encore inscrit avec Google, veuillez créer un compte avec Google',
|
||||
),
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
emit(AuthError(message: e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _onGoogleSignUpRequested(
|
||||
AuthGoogleSignUpRequested event,
|
||||
Emitter<AuthState> emit,
|
||||
) async {
|
||||
// This method can be implemented if needed for Google sign-up specific logic.
|
||||
emit(AuthLoading());
|
||||
try {
|
||||
final user = await _authRepository.signUpWithGoogle(event.phoneNumber);
|
||||
|
||||
if (user != null) {
|
||||
emit(AuthAuthenticated(user: user));
|
||||
} else {
|
||||
emit(const AuthError(message: 'Failed to create account with Google'));
|
||||
}
|
||||
} catch (e) {
|
||||
emit(AuthError(message: e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _onAppleSignUpRequested(
|
||||
AuthAppleSignUpRequested event,
|
||||
Emitter<AuthState> emit,
|
||||
) async {
|
||||
// This method can be implemented if needed for Apple sign-up specific logic.
|
||||
emit(AuthLoading());
|
||||
try {
|
||||
final user = await _authRepository.signUpWithApple(event.phoneNumber);
|
||||
|
||||
if (user != null) {
|
||||
emit(AuthAuthenticated(user: user));
|
||||
} else {
|
||||
emit(const AuthError(message: 'Failed to create account with Apple'));
|
||||
}
|
||||
} catch (e) {
|
||||
emit(AuthError(message: e.toString()));
|
||||
@@ -154,7 +203,7 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
|
||||
}
|
||||
|
||||
/// Handles [AuthAppleSignInRequested] events.
|
||||
///
|
||||
///
|
||||
/// Attempts to sign in the user using Apple authentication.
|
||||
/// Emits [AuthAuthenticated] on success or [AuthError] on failure.
|
||||
Future<void> _onAppleSignInRequested(
|
||||
@@ -162,14 +211,19 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
|
||||
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'));
|
||||
emit(
|
||||
const AuthError(
|
||||
message:
|
||||
'Utilisateur n\'est pas encore inscrit avec Apple, veuillez créer un compte avec Apple',
|
||||
),
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
emit(AuthError(message: e.toString()));
|
||||
@@ -177,7 +231,7 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
|
||||
}
|
||||
|
||||
/// Handles [AuthSignOutRequested] events.
|
||||
///
|
||||
///
|
||||
/// Signs out the current user and emits [AuthUnauthenticated].
|
||||
Future<void> _onSignOutRequested(
|
||||
AuthSignOutRequested event,
|
||||
@@ -188,7 +242,7 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
|
||||
}
|
||||
|
||||
/// Handles [AuthPasswordResetRequested] events.
|
||||
///
|
||||
///
|
||||
/// Sends a password reset email to the specified email address.
|
||||
/// Emits [AuthPasswordResetSent] on success or [AuthError] on failure.
|
||||
Future<void> _onPasswordResetRequested(
|
||||
@@ -202,4 +256,4 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
|
||||
emit(AuthError(message: e.toString()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
|
||||
/// Abstract base class for all authentication-related events.
|
||||
///
|
||||
///
|
||||
/// This class extends [Equatable] to enable value equality for event comparison.
|
||||
/// All authentication events in the application should inherit from this class.
|
||||
abstract class AuthEvent extends Equatable {
|
||||
@@ -13,59 +13,60 @@ abstract class AuthEvent extends Equatable {
|
||||
}
|
||||
|
||||
/// Event to check the current authentication status.
|
||||
///
|
||||
///
|
||||
/// This event is typically dispatched when the app starts to determine
|
||||
/// if a user is already authenticated.
|
||||
class AuthCheckRequested extends AuthEvent {}
|
||||
|
||||
/// Event to request user sign-in with email and password.
|
||||
///
|
||||
///
|
||||
/// This event contains the user's credentials and triggers the authentication
|
||||
/// process when dispatched to the [AuthBloc].
|
||||
class AuthSignInRequested extends AuthEvent {
|
||||
/// The user's email address.
|
||||
final String email;
|
||||
|
||||
|
||||
/// The user's password.
|
||||
final String password;
|
||||
|
||||
/// Creates a new [AuthSignInRequested] event.
|
||||
///
|
||||
///
|
||||
/// Both [email] and [password] are required parameters.
|
||||
const AuthSignInRequested({
|
||||
required this.email,
|
||||
required this.password,
|
||||
});
|
||||
const AuthSignInRequested({required this.email, required this.password});
|
||||
|
||||
@override
|
||||
List<Object?> get props => [email, password];
|
||||
}
|
||||
|
||||
/// Event to request user registration with email, password, and personal information.
|
||||
///
|
||||
///
|
||||
/// This event contains all necessary information to create a new user account
|
||||
/// and triggers the registration process when dispatched to the [AuthBloc].
|
||||
class AuthSignUpRequested extends AuthEvent {
|
||||
/// The user's email address.
|
||||
final String email;
|
||||
|
||||
|
||||
/// The user's password.
|
||||
final String password;
|
||||
|
||||
|
||||
/// The user's last name.
|
||||
final String nom;
|
||||
|
||||
|
||||
/// The user's first name.
|
||||
final String prenom;
|
||||
|
||||
/// The user's phone number.
|
||||
final String phoneNumber;
|
||||
|
||||
/// Creates a new [AuthSignUpRequested] event.
|
||||
///
|
||||
///
|
||||
/// All parameters are required for user registration.
|
||||
const AuthSignUpRequested({
|
||||
required this.email,
|
||||
required this.password,
|
||||
required this.nom,
|
||||
required this.prenom,
|
||||
required this.phoneNumber,
|
||||
});
|
||||
|
||||
@override
|
||||
@@ -73,23 +74,49 @@ class AuthSignUpRequested extends AuthEvent {
|
||||
}
|
||||
|
||||
/// Event to request user sign-in using Google authentication.
|
||||
///
|
||||
///
|
||||
/// This event triggers the Google sign-in flow when dispatched to the [AuthBloc].
|
||||
class AuthGoogleSignInRequested extends AuthEvent {}
|
||||
|
||||
class AuthGoogleSignUpRequested extends AuthEvent {
|
||||
/// The user's phone number.
|
||||
final String phoneNumber;
|
||||
|
||||
/// Creates a new [AuthGoogleSignUpRequested] event.
|
||||
///
|
||||
/// The [phoneNumber] parameter is required.
|
||||
const AuthGoogleSignUpRequested({required this.phoneNumber});
|
||||
|
||||
@override
|
||||
List<Object?> get props => [phoneNumber];
|
||||
}
|
||||
|
||||
/// Event to request user sign-in using Apple authentication.
|
||||
///
|
||||
///
|
||||
/// This event triggers the Apple sign-in flow when dispatched to the [AuthBloc].
|
||||
class AuthAppleSignInRequested extends AuthEvent {}
|
||||
|
||||
class AuthAppleSignUpRequested extends AuthEvent {
|
||||
/// The user's phone number.
|
||||
final String phoneNumber;
|
||||
|
||||
/// Creates a new [AuthAppleSignUpRequested] event.
|
||||
///
|
||||
/// The [phoneNumber] parameter is required.
|
||||
const AuthAppleSignUpRequested({required this.phoneNumber});
|
||||
|
||||
@override
|
||||
List<Object?> get props => [phoneNumber];
|
||||
}
|
||||
|
||||
/// Event to request user sign-out.
|
||||
///
|
||||
///
|
||||
/// This event triggers the sign-out process and clears the user session
|
||||
/// when dispatched to the [AuthBloc].
|
||||
class AuthSignOutRequested extends AuthEvent {}
|
||||
|
||||
/// Event to request a password reset for a user account.
|
||||
///
|
||||
///
|
||||
/// This event triggers the password reset process by sending a reset email
|
||||
/// to the specified email address.
|
||||
class AuthPasswordResetRequested extends AuthEvent {
|
||||
@@ -97,10 +124,10 @@ class AuthPasswordResetRequested extends AuthEvent {
|
||||
final String email;
|
||||
|
||||
/// Creates a new [AuthPasswordResetRequested] event.
|
||||
///
|
||||
///
|
||||
/// The [email] parameter is required.
|
||||
const AuthPasswordResetRequested({required this.email});
|
||||
|
||||
@override
|
||||
List<Object?> get props => [email];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user