@@ -27,13 +27,13 @@ class AuthenticationBloc
27
27
/// {@macro authentication_bloc}
28
28
AuthenticationBloc ({required HtAuthRepository authenticationRepository})
29
29
: _authenticationRepository = authenticationRepository,
30
- super (AuthenticationInitial ()) {
30
+ super (const AuthenticationState ()) {
31
31
// Listen to authentication state changes from the repository
32
32
_userAuthSubscription = _authenticationRepository.authStateChanges.listen (
33
- (user) => add (_AuthenticationUserChanged (user: user)),
33
+ (user) => add (_AuthenticationStatusChanged (user: user)),
34
34
);
35
35
36
- on < _AuthenticationUserChanged > (_onAuthenticationUserChanged );
36
+ on < _AuthenticationStatusChanged > (_onAuthenticationStatusChanged );
37
37
on < AuthenticationRequestSignInCodeRequested > (
38
38
_onAuthenticationRequestSignInCodeRequested,
39
39
);
@@ -44,15 +44,25 @@ class AuthenticationBloc
44
44
final HtAuthRepository _authenticationRepository;
45
45
late final StreamSubscription <User ?> _userAuthSubscription;
46
46
47
- /// Handles [_AuthenticationUserChanged ] events.
48
- Future <void > _onAuthenticationUserChanged (
49
- _AuthenticationUserChanged event,
47
+ /// Handles [_AuthenticationStatusChanged ] events.
48
+ Future <void > _onAuthenticationStatusChanged (
49
+ _AuthenticationStatusChanged event,
50
50
Emitter <AuthenticationState > emit,
51
51
) async {
52
52
if (event.user != null ) {
53
- emit (AuthenticationAuthenticated (user: event.user! ));
53
+ emit (
54
+ state.copyWith (
55
+ status: AuthenticationStatus .authenticated,
56
+ user: event.user,
57
+ ),
58
+ );
54
59
} else {
55
- emit (AuthenticationUnauthenticated ());
60
+ emit (
61
+ state.copyWith (
62
+ status: AuthenticationStatus .unauthenticated,
63
+ user: null ,
64
+ ),
65
+ );
56
66
}
57
67
}
58
68
@@ -63,37 +73,87 @@ class AuthenticationBloc
63
73
) async {
64
74
// Validate email format (basic check)
65
75
if (event.email.isEmpty || ! event.email.contains ('@' )) {
66
- emit (const AuthenticationFailure ('Please enter a valid email address.' ));
76
+ emit (
77
+ state.copyWith (
78
+ status: AuthenticationStatus .failure,
79
+ errorMessage: 'Please enter a valid email address.' ,
80
+ ),
81
+ );
67
82
return ;
68
83
}
69
- emit (AuthenticationRequestCodeLoading ( ));
84
+ emit (state. copyWith (status : AuthenticationStatus .requestCodeLoading ));
70
85
try {
71
86
await _authenticationRepository.requestSignInCode (
72
87
event.email,
73
88
isDashboardLogin: true ,
74
89
);
75
- emit (AuthenticationCodeSentSuccess (email: event.email));
90
+ emit (
91
+ state.copyWith (
92
+ status: AuthenticationStatus .codeSentSuccess,
93
+ email: event.email,
94
+ ),
95
+ );
76
96
} on InvalidInputException catch (e) {
77
- emit (AuthenticationFailure ('Invalid input: ${e .message }' ));
97
+ emit (
98
+ state.copyWith (
99
+ status: AuthenticationStatus .failure,
100
+ errorMessage: 'Invalid input: ${e .message }' ,
101
+ ),
102
+ );
78
103
} on UnauthorizedException catch (e) {
79
- emit (AuthenticationFailure (e.message));
104
+ emit (
105
+ state.copyWith (
106
+ status: AuthenticationStatus .failure,
107
+ errorMessage: e.message,
108
+ ),
109
+ );
80
110
} on ForbiddenException catch (e) {
81
- emit (AuthenticationFailure (e.message));
111
+ emit (
112
+ state.copyWith (
113
+ status: AuthenticationStatus .failure,
114
+ errorMessage: e.message,
115
+ ),
116
+ );
82
117
} on NetworkException catch (_) {
83
- emit (const AuthenticationFailure ('Network error occurred.' ));
118
+ emit (
119
+ state.copyWith (
120
+ status: AuthenticationStatus .failure,
121
+ errorMessage: 'Network error occurred.' ,
122
+ ),
123
+ );
84
124
} on ServerException catch (e) {
85
- emit (AuthenticationFailure ('Server error: ${e .message }' ));
125
+ emit (
126
+ state.copyWith (
127
+ status: AuthenticationStatus .failure,
128
+ errorMessage: 'Server error: ${e .message }' ,
129
+ ),
130
+ );
86
131
} on OperationFailedException catch (e) {
87
- emit (AuthenticationFailure ('Operation failed: ${e .message }' ));
132
+ emit (
133
+ state.copyWith (
134
+ status: AuthenticationStatus .failure,
135
+ errorMessage: 'Operation failed: ${e .message }' ,
136
+ ),
137
+ );
88
138
} on HtHttpException catch (e) {
89
139
// Catch any other HtHttpException subtypes
90
140
final message = e.message.isNotEmpty
91
141
? e.message
92
142
: 'An unspecified HTTP error occurred.' ;
93
- emit (AuthenticationFailure ('HTTP error: $message ' ));
143
+ emit (
144
+ state.copyWith (
145
+ status: AuthenticationStatus .failure,
146
+ errorMessage: 'HTTP error: $message ' ,
147
+ ),
148
+ );
94
149
} catch (e) {
95
150
// Catch any other unexpected errors
96
- emit (AuthenticationFailure ('An unexpected error occurred: $e ' ));
151
+ emit (
152
+ state.copyWith (
153
+ status: AuthenticationStatus .failure,
154
+ errorMessage: 'An unexpected error occurred: $e ' ,
155
+ ),
156
+ );
97
157
// Optionally log the stackTrace here
98
158
}
99
159
}
@@ -103,33 +163,73 @@ class AuthenticationBloc
103
163
AuthenticationVerifyCodeRequested event,
104
164
Emitter <AuthenticationState > emit,
105
165
) async {
106
- emit (AuthenticationLoading ( ));
166
+ emit (state. copyWith (status : AuthenticationStatus .loading ));
107
167
try {
108
168
await _authenticationRepository.verifySignInCode (
109
169
event.email,
110
170
event.code,
111
171
isDashboardLogin: true ,
112
172
);
113
- // On success, the _AuthenticationUserChanged listener will handle
173
+ // On success, the _AuthenticationStatusChanged listener will handle
114
174
// emitting AuthenticationAuthenticated.
115
175
} on InvalidInputException catch (e) {
116
- emit (AuthenticationFailure (e.message));
176
+ emit (
177
+ state.copyWith (
178
+ status: AuthenticationStatus .failure,
179
+ errorMessage: e.message,
180
+ ),
181
+ );
117
182
} on AuthenticationException catch (e) {
118
- emit (AuthenticationFailure (e.message));
183
+ emit (
184
+ state.copyWith (
185
+ status: AuthenticationStatus .failure,
186
+ errorMessage: e.message,
187
+ ),
188
+ );
119
189
} on NotFoundException catch (e) {
120
- emit (AuthenticationFailure (e.message));
190
+ emit (
191
+ state.copyWith (
192
+ status: AuthenticationStatus .failure,
193
+ errorMessage: e.message,
194
+ ),
195
+ );
121
196
} on NetworkException catch (_) {
122
- emit (const AuthenticationFailure ('Network error occurred.' ));
197
+ emit (
198
+ state.copyWith (
199
+ status: AuthenticationStatus .failure,
200
+ errorMessage: 'Network error occurred.' ,
201
+ ),
202
+ );
123
203
} on ServerException catch (e) {
124
- emit (AuthenticationFailure ('Server error: ${e .message }' ));
204
+ emit (
205
+ state.copyWith (
206
+ status: AuthenticationStatus .failure,
207
+ errorMessage: 'Server error: ${e .message }' ,
208
+ ),
209
+ );
125
210
} on OperationFailedException catch (e) {
126
- emit (AuthenticationFailure ('Operation failed: ${e .message }' ));
211
+ emit (
212
+ state.copyWith (
213
+ status: AuthenticationStatus .failure,
214
+ errorMessage: 'Operation failed: ${e .message }' ,
215
+ ),
216
+ );
127
217
} on HtHttpException catch (e) {
128
218
// Catch any other HtHttpException subtypes
129
- emit (AuthenticationFailure ('HTTP error: ${e .message }' ));
219
+ emit (
220
+ state.copyWith (
221
+ status: AuthenticationStatus .failure,
222
+ errorMessage: 'HTTP error: ${e .message }' ,
223
+ ),
224
+ );
130
225
} catch (e) {
131
226
// Catch any other unexpected errors
132
- emit (AuthenticationFailure ('An unexpected error occurred: $e ' ));
227
+ emit (
228
+ state.copyWith (
229
+ status: AuthenticationStatus .failure,
230
+ errorMessage: 'An unexpected error occurred: $e ' ,
231
+ ),
232
+ );
133
233
// Optionally log the stackTrace here
134
234
}
135
235
}
@@ -139,26 +239,47 @@ class AuthenticationBloc
139
239
AuthenticationSignOutRequested event,
140
240
Emitter <AuthenticationState > emit,
141
241
) async {
142
- emit (AuthenticationLoading ( ));
242
+ emit (state. copyWith (status : AuthenticationStatus .loading ));
143
243
try {
144
244
await _authenticationRepository.signOut ();
145
- // On success, the _AuthenticationUserChanged listener will handle
245
+ // On success, the _AuthenticationStatusChanged listener will handle
146
246
// emitting AuthenticationUnauthenticated.
147
- // No need to emit AuthenticationLoading() before calling signOut if
148
- // the authStateChanges listener handles the subsequent state update.
149
- // However, if immediate feedback is desired, it can be kept.
150
- // For now, let's assume the listener is sufficient.
151
247
} on NetworkException catch (_) {
152
- emit (const AuthenticationFailure ('Network error occurred.' ));
248
+ emit (
249
+ state.copyWith (
250
+ status: AuthenticationStatus .failure,
251
+ errorMessage: 'Network error occurred.' ,
252
+ ),
253
+ );
153
254
} on ServerException catch (e) {
154
- emit (AuthenticationFailure ('Server error: ${e .message }' ));
255
+ emit (
256
+ state.copyWith (
257
+ status: AuthenticationStatus .failure,
258
+ errorMessage: 'Server error: ${e .message }' ,
259
+ ),
260
+ );
155
261
} on OperationFailedException catch (e) {
156
- emit (AuthenticationFailure ('Operation failed: ${e .message }' ));
262
+ emit (
263
+ state.copyWith (
264
+ status: AuthenticationStatus .failure,
265
+ errorMessage: 'Operation failed: ${e .message }' ,
266
+ ),
267
+ );
157
268
} on HtHttpException catch (e) {
158
269
// Catch any other HtHttpException subtypes
159
- emit (AuthenticationFailure ('HTTP error: ${e .message }' ));
270
+ emit (
271
+ state.copyWith (
272
+ status: AuthenticationStatus .failure,
273
+ errorMessage: 'HTTP error: ${e .message }' ,
274
+ ),
275
+ );
160
276
} catch (e) {
161
- emit (AuthenticationFailure ('An unexpected error occurred: $e ' ));
277
+ emit (
278
+ state.copyWith (
279
+ status: AuthenticationStatus .failure,
280
+ errorMessage: 'An unexpected error occurred: $e ' ,
281
+ ),
282
+ );
162
283
}
163
284
}
164
285
0 commit comments