fix for resend, cooldown and a few small fixes
This commit is contained in:
91
frontend/pweb/lib/controllers/signup/confirmation.dart
Normal file
91
frontend/pweb/lib/controllers/signup/confirmation.dart
Normal file
@@ -0,0 +1,91 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
import 'package:pshared/models/auth/probe_result.dart';
|
||||
import 'package:pshared/provider/account.dart';
|
||||
|
||||
|
||||
class SignupConfirmationController extends ChangeNotifier {
|
||||
SignupConfirmationController({
|
||||
required AccountProvider accountProvider,
|
||||
Duration pollInterval = const Duration(seconds: 10),
|
||||
}) : _accountProvider = accountProvider,
|
||||
_pollInterval = pollInterval;
|
||||
|
||||
final AccountProvider _accountProvider;
|
||||
final Duration _pollInterval;
|
||||
|
||||
Timer? _pollTimer;
|
||||
bool _isChecking = false;
|
||||
bool _isAuthorized = false;
|
||||
|
||||
String? _email;
|
||||
String? _password;
|
||||
String? _locale;
|
||||
|
||||
bool get isAuthorized => _isAuthorized;
|
||||
bool get isChecking => _isChecking;
|
||||
|
||||
void startPolling({
|
||||
required String email,
|
||||
required String password,
|
||||
required String locale,
|
||||
}) {
|
||||
final trimmedEmail = email.trim();
|
||||
final trimmedPassword = password.trim();
|
||||
final trimmedLocale = locale.trim();
|
||||
if (trimmedEmail.isEmpty || trimmedPassword.isEmpty || trimmedLocale.isEmpty) {
|
||||
return;
|
||||
}
|
||||
|
||||
_email = trimmedEmail;
|
||||
_password = trimmedPassword;
|
||||
_locale = trimmedLocale;
|
||||
|
||||
_pollTimer?.cancel();
|
||||
_pollTimer = Timer.periodic(_pollInterval, (_) => _probeAuthorization());
|
||||
_probeAuthorization();
|
||||
}
|
||||
|
||||
void stopPolling() {
|
||||
_pollTimer?.cancel();
|
||||
_pollTimer = null;
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_pollTimer?.cancel();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
Future<void> _probeAuthorization() async {
|
||||
if (_isChecking || _isAuthorized) return;
|
||||
final email = _email;
|
||||
final password = _password;
|
||||
final locale = _locale;
|
||||
if (email == null || password == null || locale == null) return;
|
||||
|
||||
_setChecking(true);
|
||||
try {
|
||||
final result = await _accountProvider.probeAuthorization(
|
||||
email: email,
|
||||
password: password,
|
||||
locale: locale,
|
||||
);
|
||||
if (result == AuthProbeResult.authorized) {
|
||||
_isAuthorized = true;
|
||||
stopPolling();
|
||||
notifyListeners();
|
||||
}
|
||||
} finally {
|
||||
_setChecking(false);
|
||||
}
|
||||
}
|
||||
|
||||
void _setChecking(bool value) {
|
||||
if (_isChecking == value) return;
|
||||
_isChecking = value;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
132
frontend/pweb/lib/controllers/signup/confirmation_card.dart
Normal file
132
frontend/pweb/lib/controllers/signup/confirmation_card.dart
Normal file
@@ -0,0 +1,132 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
import 'package:pshared/provider/account.dart';
|
||||
|
||||
import 'package:pweb/models/flow_status.dart';
|
||||
import 'package:pweb/models/resend/action_result.dart';
|
||||
import 'package:pweb/models/resend/avaliability.dart';
|
||||
|
||||
|
||||
class SignupConfirmationCardController extends ChangeNotifier {
|
||||
SignupConfirmationCardController({
|
||||
required AccountProvider accountProvider,
|
||||
Duration defaultCooldown = const Duration(seconds: 60),
|
||||
}) : _accountProvider = accountProvider,
|
||||
_defaultCooldown = defaultCooldown;
|
||||
|
||||
final AccountProvider _accountProvider;
|
||||
final Duration _defaultCooldown;
|
||||
|
||||
Timer? _cooldownTimer;
|
||||
DateTime? _cooldownUntil;
|
||||
int _cooldownRemainingSeconds = 0;
|
||||
FlowStatus _resendState = FlowStatus.idle;
|
||||
String? _email;
|
||||
|
||||
int get cooldownRemainingSeconds => _cooldownRemainingSeconds;
|
||||
ResendAvailability get resendAvailability {
|
||||
final email = _email;
|
||||
if (email == null || email.isEmpty) {
|
||||
return ResendAvailability.missingEmail;
|
||||
}
|
||||
if (_resendState == FlowStatus.submitting) {
|
||||
return ResendAvailability.resending;
|
||||
}
|
||||
if (_cooldownRemainingSeconds > 0) {
|
||||
return ResendAvailability.cooldown;
|
||||
}
|
||||
return ResendAvailability.available;
|
||||
}
|
||||
|
||||
void updateEmail(String? email) {
|
||||
final trimmed = email?.trim();
|
||||
if (_email == trimmed) return;
|
||||
_email = trimmed;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void initialize({String? email}) {
|
||||
updateEmail(email);
|
||||
startDefaultCooldown();
|
||||
}
|
||||
|
||||
void startDefaultCooldown() {
|
||||
_startCooldown(_defaultCooldown);
|
||||
}
|
||||
|
||||
Future<ResendActionResult> resendVerificationEmail() async {
|
||||
switch (resendAvailability) {
|
||||
case ResendAvailability.missingEmail:
|
||||
return ResendActionResult.missingEmail;
|
||||
case ResendAvailability.cooldown:
|
||||
return ResendActionResult.cooldown;
|
||||
case ResendAvailability.resending:
|
||||
return ResendActionResult.inProgress;
|
||||
case ResendAvailability.available:
|
||||
break;
|
||||
}
|
||||
|
||||
_setResendState(FlowStatus.submitting);
|
||||
try {
|
||||
final email = _email;
|
||||
if (email == null || email.isEmpty) {
|
||||
_setResendState(FlowStatus.idle);
|
||||
return ResendActionResult.missingEmail;
|
||||
}
|
||||
await _accountProvider.resendVerificationEmail(email);
|
||||
_startCooldown(_defaultCooldown);
|
||||
return ResendActionResult.sent;
|
||||
} finally {
|
||||
_setResendState(FlowStatus.idle);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_cooldownTimer?.cancel();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _startCooldown(Duration duration) {
|
||||
_cooldownTimer?.cancel();
|
||||
_cooldownUntil = DateTime.now().add(duration);
|
||||
_syncRemaining();
|
||||
|
||||
if (_cooldownRemainingSeconds <= 0) {
|
||||
_cooldownUntil = null;
|
||||
notifyListeners();
|
||||
return;
|
||||
}
|
||||
|
||||
_cooldownTimer = Timer.periodic(const Duration(seconds: 1), (timer) {
|
||||
_syncRemaining();
|
||||
if (_cooldownRemainingSeconds <= 0) {
|
||||
timer.cancel();
|
||||
_cooldownUntil = null;
|
||||
notifyListeners();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void _syncRemaining() {
|
||||
final remaining = _cooldownRemaining();
|
||||
if (remaining == _cooldownRemainingSeconds) return;
|
||||
_cooldownRemainingSeconds = remaining;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
int _cooldownRemaining() {
|
||||
final until = _cooldownUntil;
|
||||
if (until == null) return 0;
|
||||
final remaining = until.difference(DateTime.now()).inSeconds;
|
||||
return remaining < 0 ? 0 : remaining;
|
||||
}
|
||||
|
||||
void _setResendState(FlowStatus state) {
|
||||
if (_resendState == state) return;
|
||||
_resendState = state;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user