@@ -26,7 +26,7 @@ class AuthenticationBloc
26
26
: _authenticationRepository = authenticationRepository,
27
27
super (AuthenticationInitial ()) {
28
28
// Listen to authentication state changes from the repository
29
- _authenticationRepository.authStateChanges.listen (
29
+ _userAuthSubscription = _authenticationRepository.authStateChanges.listen (
30
30
(user) => add (_AuthenticationUserChanged (user: user)),
31
31
);
32
32
@@ -42,6 +42,7 @@ class AuthenticationBloc
42
42
}
43
43
44
44
final HtAuthRepository _authenticationRepository;
45
+ late final StreamSubscription <User ?> _userAuthSubscription;
45
46
46
47
/// Handles [_AuthenticationUserChanged] events.
47
48
Future <void > _onAuthenticationUserChanged (
@@ -83,7 +84,8 @@ class AuthenticationBloc
83
84
emit (AuthenticationFailure ('Operation failed: ${e .message }' ));
84
85
} on HtHttpException catch (e) {
85
86
// Catch any other HtHttpException subtypes
86
- emit (AuthenticationFailure ('HTTP error: ${e .message }' ));
87
+ final message = e.message.isNotEmpty ? e.message : 'An unspecified HTTP error occurred.' ;
88
+ emit (AuthenticationFailure ('HTTP error: $message ' ));
87
89
} catch (e) {
88
90
// Catch any other unexpected errors
89
91
emit (AuthenticationFailure ('An unexpected error occurred: $e ' ));
@@ -102,9 +104,9 @@ class AuthenticationBloc
102
104
// On success, the _AuthenticationUserChanged listener will handle
103
105
// emitting AuthenticationAuthenticated.
104
106
} on InvalidInputException catch (e) {
105
- emit (AuthenticationFailure ('Invalid input: ${ e .message }' ));
107
+ emit (AuthenticationFailure (e.message)); // Use specific error message
106
108
} on AuthenticationException catch (e) {
107
- emit (AuthenticationFailure ('Authentication failed: ${ e .message }' ));
109
+ emit (AuthenticationFailure (e.message)); // Use specific error message
108
110
} on NetworkException catch (_) {
109
111
emit (const AuthenticationFailure ('Network error occurred.' ));
110
112
} on ServerException catch (e) {
@@ -155,6 +157,10 @@ class AuthenticationBloc
155
157
await _authenticationRepository.signOut ();
156
158
// On success, the _AuthenticationUserChanged listener will handle
157
159
// emitting AuthenticationUnauthenticated.
160
+ // No need to emit AuthenticationLoading() before calling signOut if
161
+ // the authStateChanges listener handles the subsequent state update.
162
+ // However, if immediate feedback is desired, it can be kept.
163
+ // For now, let's assume the listener is sufficient.
158
164
} on NetworkException catch (_) {
159
165
emit (const AuthenticationFailure ('Network error occurred.' ));
160
166
} on ServerException catch (e) {
@@ -168,4 +174,10 @@ class AuthenticationBloc
168
174
emit (AuthenticationFailure ('An unexpected error occurred: $e ' ));
169
175
}
170
176
}
177
+
178
+ @override
179
+ Future <void > close () {
180
+ _userAuthSubscription.cancel ();
181
+ return super .close ();
182
+ }
171
183
}
0 commit comments