Implemented cooldown before User is able to resend confirmation code for 2fa
This commit is contained in:
@@ -13,9 +13,15 @@ class ResendCodeButton extends StatelessWidget {
|
||||
Widget build(BuildContext context) {
|
||||
final theme = Theme.of(context);
|
||||
final localizations = AppLocalizations.of(context)!;
|
||||
final provider = context.watch<TwoFactorProvider>();
|
||||
final isDisabled = provider.isCooldownActive || provider.isResending;
|
||||
|
||||
final label = provider.isCooldownActive
|
||||
? '${localizations.twoFactorResend} (${_formatCooldown(provider.cooldownRemainingSeconds)})'
|
||||
: localizations.twoFactorResend;
|
||||
|
||||
return TextButton(
|
||||
onPressed: () => context.read<TwoFactorProvider>().resendCode(),
|
||||
onPressed: isDisabled ? null : () => provider.resendCode(),
|
||||
style: TextButton.styleFrom(
|
||||
padding: EdgeInsets.zero,
|
||||
minimumSize: const Size(0, 0),
|
||||
@@ -26,7 +32,25 @@ class ResendCodeButton extends StatelessWidget {
|
||||
decoration: TextDecoration.underline,
|
||||
),
|
||||
),
|
||||
child: Text(localizations.twoFactorResend),
|
||||
child: provider.isResending
|
||||
? SizedBox(
|
||||
width: 16,
|
||||
height: 16,
|
||||
child: CircularProgressIndicator(
|
||||
strokeWidth: 2,
|
||||
color: theme.colorScheme.primary,
|
||||
),
|
||||
)
|
||||
: Text(label),
|
||||
);
|
||||
}
|
||||
|
||||
String _formatCooldown(int seconds) {
|
||||
final minutes = seconds ~/ 60;
|
||||
final remainingSeconds = seconds % 60;
|
||||
if (minutes > 0) {
|
||||
return '$minutes:${remainingSeconds.toString().padLeft(2, '0')}';
|
||||
}
|
||||
return remainingSeconds.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:logging/logging.dart';
|
||||
@@ -14,15 +16,21 @@ class TwoFactorProvider extends ChangeNotifier {
|
||||
TwoFactorProvider();
|
||||
|
||||
bool _isSubmitting = false;
|
||||
bool _isResending = false;
|
||||
bool _hasError = false;
|
||||
bool _verificationSuccess = false;
|
||||
String? _errorMessage;
|
||||
String? _currentPendingToken;
|
||||
Timer? _cooldownTimer;
|
||||
int _cooldownRemainingSeconds = 0;
|
||||
|
||||
bool get isSubmitting => _isSubmitting;
|
||||
bool get isResending => _isResending;
|
||||
bool get hasError => _hasError;
|
||||
bool get verificationSuccess => _verificationSuccess;
|
||||
String? get errorMessage => _errorMessage;
|
||||
int get cooldownRemainingSeconds => _cooldownRemainingSeconds;
|
||||
bool get isCooldownActive => _cooldownRemainingSeconds > 0;
|
||||
PendingLogin? get pendingLogin => _accountProvider.pendingLogin;
|
||||
|
||||
void update(AccountProvider accountProvider) {
|
||||
@@ -33,6 +41,7 @@ class TwoFactorProvider extends ChangeNotifier {
|
||||
_resetState();
|
||||
_currentPendingToken = token;
|
||||
}
|
||||
_syncCooldown(pending);
|
||||
}
|
||||
|
||||
Future<void> submitCode(String code) async {
|
||||
@@ -70,12 +79,23 @@ class TwoFactorProvider extends ChangeNotifier {
|
||||
_logger.warning('No pending login to resend code for');
|
||||
return;
|
||||
}
|
||||
if (_isResending || isCooldownActive) return;
|
||||
|
||||
_isResending = true;
|
||||
_hasError = false;
|
||||
_errorMessage = null;
|
||||
notifyListeners();
|
||||
|
||||
try {
|
||||
await VerificationService.resendLoginCode(pending);
|
||||
final confirmation = await VerificationService.resendLoginCode(pending);
|
||||
_accountProvider.updatePendingLogin(confirmation);
|
||||
_startCooldown(confirmation.cooldownSeconds);
|
||||
} catch (e) {
|
||||
_logger.warning('Failed to resend login code', e);
|
||||
_hasError = true;
|
||||
_errorMessage = e.toString();
|
||||
} finally {
|
||||
_isResending = false;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
@@ -87,9 +107,71 @@ class TwoFactorProvider extends ChangeNotifier {
|
||||
|
||||
void _resetState() {
|
||||
_isSubmitting = false;
|
||||
_isResending = false;
|
||||
_hasError = false;
|
||||
_errorMessage = null;
|
||||
_verificationSuccess = false;
|
||||
_stopCooldown();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void _syncCooldown(PendingLogin? pending) {
|
||||
if (pending == null) {
|
||||
_stopCooldown(notify: _cooldownRemainingSeconds != 0);
|
||||
return;
|
||||
}
|
||||
|
||||
final remaining = pending.cooldownRemainingSeconds;
|
||||
if (remaining <= 0) {
|
||||
_stopCooldown(notify: _cooldownRemainingSeconds != 0);
|
||||
return;
|
||||
}
|
||||
|
||||
if (_cooldownRemainingSeconds != remaining) {
|
||||
_startCooldown(remaining);
|
||||
}
|
||||
}
|
||||
|
||||
void _startCooldown(int seconds) {
|
||||
_cooldownTimer?.cancel();
|
||||
_cooldownRemainingSeconds = seconds;
|
||||
|
||||
if (_cooldownRemainingSeconds <= 0) {
|
||||
_cooldownTimer = null;
|
||||
notifyListeners();
|
||||
return;
|
||||
}
|
||||
|
||||
_cooldownTimer = Timer.periodic(const Duration(seconds: 1), (timer) {
|
||||
if (_cooldownRemainingSeconds <= 1) {
|
||||
_cooldownRemainingSeconds = 0;
|
||||
_cooldownTimer?.cancel();
|
||||
_cooldownTimer = null;
|
||||
notifyListeners();
|
||||
return;
|
||||
}
|
||||
|
||||
_cooldownRemainingSeconds -= 1;
|
||||
notifyListeners();
|
||||
});
|
||||
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void _stopCooldown({bool notify = false}) {
|
||||
_cooldownTimer?.cancel();
|
||||
_cooldownTimer = null;
|
||||
final hadCooldown = _cooldownRemainingSeconds != 0;
|
||||
_cooldownRemainingSeconds = 0;
|
||||
|
||||
if (notify && hadCooldown) {
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_stopCooldown();
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user