@@ -20,6 +20,8 @@ import 'package:equatable/equatable.dart';
20
20
part 'authentication_event.dart' ;
21
21
part 'authentication_state.dart' ;
22
22
23
+ const _requestCodeCooldownDuration = Duration (seconds: 60 );
24
+
23
25
/// {@template authentication_bloc}
24
26
/// Bloc responsible for managing the authentication state of the application.
25
27
/// {@endtemplate}
@@ -40,6 +42,7 @@ class AuthenticationBloc
40
42
);
41
43
on < AuthenticationVerifyCodeRequested > (_onAuthenticationVerifyCodeRequested);
42
44
on < AuthenticationSignOutRequested > (_onAuthenticationSignOutRequested);
45
+ on < AuthenticationCooldownCompleted > (_onAuthenticationCooldownCompleted);
43
46
}
44
47
45
48
final AuthRepository _authenticationRepository;
@@ -72,18 +75,32 @@ class AuthenticationBloc
72
75
AuthenticationRequestSignInCodeRequested event,
73
76
Emitter <AuthenticationState > emit,
74
77
) async {
78
+ // Prevent request if already in cooldown
79
+ if (state.status == AuthenticationStatus .requestCodeCooldown) return ;
80
+
75
81
emit (state.copyWith (status: AuthenticationStatus .requestCodeLoading));
76
82
try {
77
83
await _authenticationRepository.requestSignInCode (
78
84
event.email,
79
85
isDashboardLogin: true ,
80
86
);
87
+ final cooldownEndTime = DateTime .now ().add (_requestCodeCooldownDuration);
81
88
emit (
82
89
state.copyWith (
83
90
status: AuthenticationStatus .codeSentSuccess,
84
91
email: event.email,
92
+ cooldownEndTime: cooldownEndTime,
85
93
),
86
94
);
95
+ // Transition to cooldown state after a brief moment
96
+ await Future <void >.delayed (const Duration (milliseconds: 100 ));
97
+ emit (state.copyWith (status: AuthenticationStatus .requestCodeCooldown));
98
+
99
+ // Start a timer to transition out of cooldown
100
+ Timer (
101
+ _requestCodeCooldownDuration,
102
+ () => add (const AuthenticationCooldownCompleted ()),
103
+ );
87
104
} on InvalidInputException catch (e) {
88
105
emit (state.copyWith (status: AuthenticationStatus .failure, exception: e));
89
106
} on UnauthorizedException catch (e) {
@@ -181,6 +198,15 @@ class AuthenticationBloc
181
198
}
182
199
}
183
200
201
+ void _onAuthenticationCooldownCompleted (
202
+ AuthenticationCooldownCompleted event,
203
+ Emitter <AuthenticationState > emit,
204
+ ) {
205
+ if (state.status == AuthenticationStatus .requestCodeCooldown) {
206
+ emit (state.copyWith (status: AuthenticationStatus .initial));
207
+ }
208
+ }
209
+
184
210
@override
185
211
Future <void > close () {
186
212
_userAuthSubscription.cancel ();
0 commit comments