fAdd phone number support to user authentication events and methods

This commit is contained in:
Van Leemput Dayron
2025-11-05 13:09:12 +01:00
parent 5977f4d0da
commit fa7daca50a
7 changed files with 685 additions and 188 deletions

View File

@@ -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()));
}
}
}
}