Files
sendico/frontend/pweb/lib/providers/two_factor.dart

50 lines
1.3 KiB
Dart

import 'package:flutter/material.dart';
import 'package:logging/logging.dart';
import 'package:pweb/services/auth.dart';
class TwoFactorProvider extends ChangeNotifier {
static final _logger = Logger('provider.two_factor');
final AuthenticationService _authService;
TwoFactorProvider({AuthenticationService? authService}) : _authService = authService ?? AuthenticationService();
bool _isSubmitting = false;
bool _hasError = false;
bool _verificationSuccess = false;
String? _errorMessage;
bool get isSubmitting => _isSubmitting;
bool get hasError => _hasError;
bool get verificationSuccess => _verificationSuccess;
String? get errorMessage => _errorMessage;
Future<void> submitCode(String code) async {
_isSubmitting = true;
_hasError = false;
_errorMessage = null;
_verificationSuccess = false;
notifyListeners();
try {
final isValid = await _authService.verifyTwoFactorCode(code);
_verificationSuccess = isValid;
} catch (e) {
_hasError = true;
_errorMessage = e.toString();
_logger.warning('Failed to verify code', e);
} finally {
_isSubmitting = false;
notifyListeners();
}
}
Future<void> resendCode() async {
_logger.fine('Resending mock two-factor code');
_hasError = false;
_errorMessage = null;
notifyListeners();
}
}