@@ -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 (
@@ -102,9 +103,9 @@ class AuthenticationBloc
102
103
// On success, the _AuthenticationUserChanged listener will handle
103
104
// emitting AuthenticationAuthenticated.
104
105
} on InvalidInputException catch (e) {
105
- emit (AuthenticationFailure ('Invalid input: ${ e .message }' ));
106
+ emit (AuthenticationFailure (e.message)); // Use specific error message
106
107
} on AuthenticationException catch (e) {
107
- emit (AuthenticationFailure ('Authentication failed: ${ e .message }' ));
108
+ emit (AuthenticationFailure (e.message)); // Use specific error message
108
109
} on NetworkException catch (_) {
109
110
emit (const AuthenticationFailure ('Network error occurred.' ));
110
111
} on ServerException catch (e) {
@@ -155,6 +156,10 @@ class AuthenticationBloc
155
156
await _authenticationRepository.signOut ();
156
157
// On success, the _AuthenticationUserChanged listener will handle
157
158
// 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.
158
163
} on NetworkException catch (_) {
159
164
emit (const AuthenticationFailure ('Network error occurred.' ));
160
165
} on ServerException catch (e) {
@@ -168,4 +173,10 @@ class AuthenticationBloc
168
173
emit (AuthenticationFailure ('An unexpected error occurred: $e ' ));
169
174
}
170
175
}
176
+
177
+ @override
178
+ Future <void > close () {
179
+ _userAuthSubscription.cancel ();
180
+ return super .close ();
181
+ }
171
182
}
0 commit comments