Skip to content

Commit c435c82

Browse files
committed
feat(authentication): implement cooldown for sign-in code requests
- Add cooldown functionality to prevent frequent sign-in code requests - Introduce new event AuthenticationCooldownCompleted - Update AuthenticationState to include cooldownEndTime - Implement logic to check cooldown status before processing new code requests - Set cooldown duration to 60 seconds after successful code request
1 parent 1f7d51a commit c435c82

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

lib/authentication/bloc/authentication_bloc.dart

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import 'package:equatable/equatable.dart';
88
part 'authentication_event.dart';
99
part 'authentication_state.dart';
1010

11+
const _requestCodeCooldownDuration = Duration(seconds: 60);
12+
1113
/// {@template authentication_bloc}
1214
/// Bloc responsible for managing the authentication state of the application.
1315
/// {@endtemplate}
@@ -31,6 +33,7 @@ class AuthenticationBloc
3133
_onAuthenticationAnonymousSignInRequested,
3234
);
3335
on<AuthenticationSignOutRequested>(_onAuthenticationSignOutRequested);
36+
on<AuthenticationCooldownCompleted>(_onAuthenticationCooldownCompleted);
3437
}
3538

3639
final AuthRepository _authenticationRepository;
@@ -63,15 +66,27 @@ class AuthenticationBloc
6366
AuthenticationRequestSignInCodeRequested event,
6467
Emitter<AuthenticationState> emit,
6568
) async {
69+
if (state.cooldownEndTime != null &&
70+
state.cooldownEndTime!.isAfter(DateTime.now())) {
71+
return;
72+
}
73+
6674
emit(state.copyWith(status: AuthenticationStatus.requestCodeInProgress));
6775
try {
6876
await _authenticationRepository.requestSignInCode(event.email);
77+
final cooldownEndTime = DateTime.now().add(_requestCodeCooldownDuration);
6978
emit(
7079
state.copyWith(
7180
status: AuthenticationStatus.requestCodeSuccess,
7281
email: event.email,
82+
cooldownEndTime: cooldownEndTime,
7383
),
7484
);
85+
86+
Timer(
87+
_requestCodeCooldownDuration,
88+
() => add(const AuthenticationCooldownCompleted()),
89+
);
7590
} on HttpException catch (e) {
7691
emit(state.copyWith(status: AuthenticationStatus.failure, exception: e));
7792
} catch (e) {
@@ -155,4 +170,16 @@ class AuthenticationBloc
155170
_userAuthSubscription.cancel();
156171
return super.close();
157172
}
173+
174+
void _onAuthenticationCooldownCompleted(
175+
AuthenticationCooldownCompleted event,
176+
Emitter<AuthenticationState> emit,
177+
) {
178+
emit(
179+
state.copyWith(
180+
status: AuthenticationStatus.initial,
181+
clearCooldownEndTime: true,
182+
),
183+
);
184+
}
158185
}

0 commit comments

Comments
 (0)