Got rid of bools in 2fa provider

This commit is contained in:
Arseni
2025-12-24 16:26:22 +03:00
parent 964e90767d
commit 512f25f74f
3 changed files with 25 additions and 30 deletions

View File

@@ -0,0 +1 @@
enum FlowStatus { idle, submitting, resending, success, error }

View File

@@ -8,6 +8,8 @@ import 'package:pshared/models/auth/pending_login.dart';
import 'package:pshared/provider/account.dart';
import 'package:pshared/service/verification.dart';
import 'package:pweb/models/flow_status.dart';
class TwoFactorProvider extends ChangeNotifier {
static final _logger = Logger('provider.two_factor');
@@ -15,19 +17,17 @@ class TwoFactorProvider extends ChangeNotifier {
TwoFactorProvider();
bool _isSubmitting = false;
bool _isResending = false;
bool _hasError = false;
bool _verificationSuccess = false;
FlowStatus _status = FlowStatus.idle;
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;
FlowStatus get status => _status;
bool get isSubmitting => _status == FlowStatus.submitting;
bool get isResending => _status == FlowStatus.resending;
bool get hasError => _status == FlowStatus.error;
bool get verificationSuccess => _status == FlowStatus.success;
String? get errorMessage => _errorMessage;
int get cooldownRemainingSeconds => _cooldownRemainingSeconds;
bool get isCooldownActive => _cooldownRemainingSeconds > 0;
@@ -45,11 +45,8 @@ class TwoFactorProvider extends ChangeNotifier {
}
Future<void> submitCode(String code) async {
_isSubmitting = true;
_hasError = false;
_errorMessage = null;
_verificationSuccess = false;
notifyListeners();
_setStatus(FlowStatus.submitting);
try {
final pending = _accountProvider.pendingLogin;
@@ -61,15 +58,12 @@ class TwoFactorProvider extends ChangeNotifier {
code: code,
);
_accountProvider.completePendingLogin(account);
_verificationSuccess = true;
_currentPendingToken = null;
_setStatus(FlowStatus.success);
} catch (e) {
_hasError = true;
_errorMessage = e.toString();
_logger.warning('Failed to verify code: ${e.toString()}', e);
} finally {
_isSubmitting = false;
notifyListeners();
_setStatus(FlowStatus.error);
}
}
@@ -79,24 +73,20 @@ class TwoFactorProvider extends ChangeNotifier {
_logger.warning('No pending login to resend code for');
return;
}
if (_isResending || isCooldownActive) return;
if (isResending || isCooldownActive) return;
_isResending = true;
_hasError = false;
_errorMessage = null;
notifyListeners();
_setStatus(FlowStatus.resending);
try {
final confirmation = await VerificationService.resendLoginCode(pending);
_accountProvider.updatePendingLogin(confirmation);
_startCooldown(confirmation.cooldownSeconds);
_setStatus(FlowStatus.idle);
} catch (e) {
_logger.warning('Failed to resend login code', e);
_hasError = true;
_errorMessage = e.toString();
} finally {
_isResending = false;
notifyListeners();
_setStatus(FlowStatus.error);
}
}
@@ -106,11 +96,8 @@ class TwoFactorProvider extends ChangeNotifier {
}
void _resetState() {
_isSubmitting = false;
_isResending = false;
_hasError = false;
_status = FlowStatus.idle;
_errorMessage = null;
_verificationSuccess = false;
_stopCooldown();
notifyListeners();
}
@@ -169,6 +156,11 @@ class TwoFactorProvider extends ChangeNotifier {
}
}
void _setStatus(FlowStatus status) {
_status = status;
notifyListeners();
}
@override
void dispose() {
_stopCooldown();