Skip to content

Commit 66b8797

Browse files
committed
refactor(auth): remove requestCodeCooldown status
Refactored the authentication BLoC and UI to rely solely on the `cooldownEndTime` timestamp for managing the sign-in code request cooldown. This removes the redundant `requestCodeCooldown` from the `AuthenticationStatus` enum, simplifying the state machine and establishing a single source of truth for the cooldown logic. This change fixes the bug where the cooldown timer would not display correctly after a failed verification and makes the implementation more robust and maintainable.
1 parent fd82b19 commit 66b8797

File tree

3 files changed

+14
-17
lines changed

3 files changed

+14
-17
lines changed

lib/authentication/bloc/authentication_bloc.dart

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,10 @@ class AuthenticationBloc
7676
Emitter<AuthenticationState> emit,
7777
) async {
7878
// Prevent request if already in cooldown
79-
if (state.status == AuthenticationStatus.requestCodeCooldown) return;
79+
if (state.cooldownEndTime != null &&
80+
state.cooldownEndTime!.isAfter(DateTime.now())) {
81+
return;
82+
}
8083

8184
emit(state.copyWith(status: AuthenticationStatus.requestCodeLoading));
8285
try {
@@ -92,9 +95,6 @@ class AuthenticationBloc
9295
cooldownEndTime: cooldownEndTime,
9396
),
9497
);
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));
9898

9999
// Start a timer to transition out of cooldown
100100
Timer(
@@ -202,14 +202,12 @@ class AuthenticationBloc
202202
AuthenticationCooldownCompleted event,
203203
Emitter<AuthenticationState> emit,
204204
) {
205-
if (state.status == AuthenticationStatus.requestCodeCooldown) {
206-
emit(
207-
state.copyWith(
208-
status: AuthenticationStatus.initial,
209-
clearCooldownEndTime: true,
210-
),
211-
);
212-
}
205+
emit(
206+
state.copyWith(
207+
status: AuthenticationStatus.initial,
208+
clearCooldownEndTime: true,
209+
),
210+
);
213211
}
214212

215213
@override

lib/authentication/bloc/authentication_state.dart

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@ enum AuthenticationStatus {
2222
/// The sign-in code was sent successfully.
2323
codeSentSuccess,
2424

25-
/// The user is in a cooldown period after requesting a code.
26-
requestCodeCooldown,
27-
2825
/// An authentication operation failed.
2926
failure,
3027
}

lib/authentication/view/request_code_page.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,11 @@ class _EmailLinkFormState extends State<_EmailLinkForm> {
248248
final colorScheme = Theme.of(context).colorScheme;
249249

250250
return BlocListener<AuthenticationBloc, AuthenticationState>(
251+
listenWhen: (previous, current) =>
252+
previous.cooldownEndTime != current.cooldownEndTime,
251253
listener: (context, state) {
252-
if (state.status == AuthenticationStatus.requestCodeCooldown &&
253-
state.cooldownEndTime != null) {
254+
if (state.cooldownEndTime != null &&
255+
state.cooldownEndTime!.isAfter(DateTime.now())) {
254256
_cooldownTimer?.cancel();
255257
_startCooldownTimer(state.cooldownEndTime!);
256258
}

0 commit comments

Comments
 (0)