Skip to content

Commit 6be8dfa

Browse files
committed
fix(auth): improve error handling and resource mgmt
- Use specific error messages - Cancel auth subscription on close - Removed redundant loading state
1 parent 9eeae0b commit 6be8dfa

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

lib/authentication/bloc/authentication_bloc.dart

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class AuthenticationBloc
2626
: _authenticationRepository = authenticationRepository,
2727
super(AuthenticationInitial()) {
2828
// Listen to authentication state changes from the repository
29-
_authenticationRepository.authStateChanges.listen(
29+
_userAuthSubscription = _authenticationRepository.authStateChanges.listen(
3030
(user) => add(_AuthenticationUserChanged(user: user)),
3131
);
3232

@@ -42,6 +42,7 @@ class AuthenticationBloc
4242
}
4343

4444
final HtAuthRepository _authenticationRepository;
45+
late final StreamSubscription<User?> _userAuthSubscription;
4546

4647
/// Handles [_AuthenticationUserChanged] events.
4748
Future<void> _onAuthenticationUserChanged(
@@ -102,9 +103,9 @@ class AuthenticationBloc
102103
// On success, the _AuthenticationUserChanged listener will handle
103104
// emitting AuthenticationAuthenticated.
104105
} on InvalidInputException catch (e) {
105-
emit(AuthenticationFailure('Invalid input: ${e.message}'));
106+
emit(AuthenticationFailure(e.message)); // Use specific error message
106107
} on AuthenticationException catch (e) {
107-
emit(AuthenticationFailure('Authentication failed: ${e.message}'));
108+
emit(AuthenticationFailure(e.message)); // Use specific error message
108109
} on NetworkException catch (_) {
109110
emit(const AuthenticationFailure('Network error occurred.'));
110111
} on ServerException catch (e) {
@@ -155,6 +156,10 @@ class AuthenticationBloc
155156
await _authenticationRepository.signOut();
156157
// On success, the _AuthenticationUserChanged listener will handle
157158
// emitting AuthenticationUnauthenticated.
159+
// No need to emit AuthenticationLoading() before calling signOut if
160+
// the authStateChanges listener handles the subsequent state update.
161+
// However, if immediate feedback is desired, it can be kept.
162+
// For now, let's assume the listener is sufficient.
158163
} on NetworkException catch (_) {
159164
emit(const AuthenticationFailure('Network error occurred.'));
160165
} on ServerException catch (e) {
@@ -168,4 +173,10 @@ class AuthenticationBloc
168173
emit(AuthenticationFailure('An unexpected error occurred: $e'));
169174
}
170175
}
176+
177+
@override
178+
Future<void> close() {
179+
_userAuthSubscription.cancel();
180+
return super.close();
181+
}
171182
}

0 commit comments

Comments
 (0)