fix for resend, cooldown and a few small fixes
This commit is contained in:
5
frontend/pshared/lib/models/auth/probe_result.dart
Normal file
5
frontend/pshared/lib/models/auth/probe_result.dart
Normal file
@@ -0,0 +1,5 @@
|
||||
enum AuthProbeResult {
|
||||
authorized,
|
||||
notVerified,
|
||||
error,
|
||||
}
|
||||
@@ -7,10 +7,12 @@ import 'package:share_plus/share_plus.dart';
|
||||
import 'package:pshared/api/errors/unauthorized.dart';
|
||||
import 'package:pshared/api/requests/signup.dart';
|
||||
import 'package:pshared/api/requests/login_data.dart';
|
||||
import 'package:pshared/api/responses/error/server.dart';
|
||||
import 'package:pshared/api/responses/verification/response.dart';
|
||||
import 'package:pshared/config/constants.dart';
|
||||
import 'package:pshared/models/account/account.dart';
|
||||
import 'package:pshared/models/auth/login_outcome.dart';
|
||||
import 'package:pshared/models/auth/probe_result.dart';
|
||||
import 'package:pshared/models/auth/pending_login.dart';
|
||||
import 'package:pshared/models/auth/state.dart';
|
||||
import 'package:pshared/models/describable.dart';
|
||||
@@ -309,4 +311,31 @@ class AccountProvider extends ChangeNotifier {
|
||||
}
|
||||
await restore();
|
||||
}
|
||||
|
||||
Future<AuthProbeResult> probeAuthorization({
|
||||
required String email,
|
||||
required String password,
|
||||
required String locale,
|
||||
}) async {
|
||||
try {
|
||||
final outcome = await AccountService.login(LoginData.build(
|
||||
login: email,
|
||||
password: password,
|
||||
locale: locale,
|
||||
));
|
||||
if (outcome.isCompleted) {
|
||||
await AuthorizationService.logout();
|
||||
return AuthProbeResult.authorized;
|
||||
}
|
||||
if (outcome.isPending) {
|
||||
return AuthProbeResult.authorized;
|
||||
}
|
||||
return AuthProbeResult.error;
|
||||
} catch (e) {
|
||||
if (e is ErrorResponse && e.error == 'account_not_verified') {
|
||||
return AuthProbeResult.notVerified;
|
||||
}
|
||||
return AuthProbeResult.error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import 'package:pshared/provider/resource.dart';
|
||||
import 'package:pshared/service/account.dart';
|
||||
import 'package:pshared/utils/exception.dart';
|
||||
|
||||
|
||||
class EmailVerificationProvider extends ChangeNotifier {
|
||||
Resource<bool> _resource = Resource(data: null, isLoading: false);
|
||||
String? _token;
|
||||
@@ -16,6 +17,14 @@ class EmailVerificationProvider extends ChangeNotifier {
|
||||
ErrorResponse? get errorResponse =>
|
||||
_resource.error is ErrorResponse ? _resource.error as ErrorResponse : null;
|
||||
int? get errorCode => errorResponse?.code;
|
||||
bool get isTokenAlreadyUsed {
|
||||
final response = errorResponse;
|
||||
if (response == null) return false;
|
||||
if (response.code != 409 || response.error != 'data_conflict') {
|
||||
return false;
|
||||
}
|
||||
return response.details.contains('verification token has already been used');
|
||||
}
|
||||
bool get canResendVerification =>
|
||||
errorCode == 400 || errorCode == 410 || errorCode == 500;
|
||||
|
||||
|
||||
@@ -45,6 +45,9 @@ GoRouter createRouter() => GoRouter(
|
||||
email: state.extra is SignupConfirmationArgs
|
||||
? (state.extra as SignupConfirmationArgs).email
|
||||
: null,
|
||||
password: state.extra is SignupConfirmationArgs
|
||||
? (state.extra as SignupConfirmationArgs).password
|
||||
: null,
|
||||
),
|
||||
),
|
||||
GoRoute(
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -660,6 +660,8 @@
|
||||
"verificationFailed": "Verification Failed",
|
||||
"verificationStatusUnknown": "We couldn't determine the status of your verification. Please try again later.",
|
||||
"verificationStatusErrorUnknown": "Unexpected error occurred while verification. Try once again or contact support",
|
||||
"accountAlreadyVerified": "Your account has already been verified",
|
||||
"accountAlreadyVerifiedDescription": "You can now log in to access your account.",
|
||||
"accountVerified": "Account Verified!",
|
||||
"accountVerifiedDescription": "Your account has been successfully verified. You can now log in to access your account.",
|
||||
"retryVerification": "Retry Verification",
|
||||
|
||||
@@ -662,6 +662,8 @@
|
||||
"verificationFailed": "Ошибка подтверждения",
|
||||
"verificationStatusUnknown": "Не удалось определить статус подтверждения. Попробуйте позже",
|
||||
"verificationStatusErrorUnknown": "Произошла непредвиденная ошибка при подтверждении. Попробуйте еще раз или обратитесь в службу поддержки",
|
||||
"accountAlreadyVerified": "Ваш аккаунт уже подтвержден",
|
||||
"accountAlreadyVerifiedDescription": "Теперь вы можете войти, чтобы получить доступ к аккаунту.",
|
||||
"accountVerified": "Аккаунт подтвержден!",
|
||||
"accountVerifiedDescription": "Ваш аккаунт успешно подтвержден. Теперь вы можете войти, чтобы получить доступ к своему аккаунту",
|
||||
"retryVerification": "Повторить подтверждение",
|
||||
|
||||
6
frontend/pweb/lib/models/resend/action_result.dart
Normal file
6
frontend/pweb/lib/models/resend/action_result.dart
Normal file
@@ -0,0 +1,6 @@
|
||||
enum ResendActionResult {
|
||||
sent,
|
||||
missingEmail,
|
||||
cooldown,
|
||||
inProgress,
|
||||
}
|
||||
6
frontend/pweb/lib/models/resend/avaliability.dart
Normal file
6
frontend/pweb/lib/models/resend/avaliability.dart
Normal file
@@ -0,0 +1,6 @@
|
||||
enum ResendAvailability {
|
||||
available,
|
||||
cooldown,
|
||||
resending,
|
||||
missingEmail,
|
||||
}
|
||||
@@ -3,6 +3,8 @@ import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import 'package:pweb/providers/two_factor.dart';
|
||||
import 'package:pweb/utils/cooldown_format.dart';
|
||||
import 'package:pweb/widgets/resend_link.dart';
|
||||
|
||||
import 'package:pweb/generated/i18n/app_localizations.dart';
|
||||
|
||||
@@ -12,46 +14,20 @@ class ResendCodeButton extends StatelessWidget {
|
||||
|
||||
@override
|
||||
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} (${formatCooldownSeconds(provider.cooldownRemainingSeconds)})'
|
||||
: localizations.twoFactorResend;
|
||||
|
||||
return TextButton(
|
||||
onPressed: isDisabled ? null : () => provider.resendCode(),
|
||||
style: TextButton.styleFrom(
|
||||
padding: EdgeInsets.zero,
|
||||
minimumSize: const Size(0, 0),
|
||||
tapTargetSize: MaterialTapTargetSize.shrinkWrap,
|
||||
alignment: Alignment.centerLeft,
|
||||
foregroundColor: theme.colorScheme.primary,
|
||||
textStyle: theme.textTheme.bodyMedium?.copyWith(
|
||||
decoration: TextDecoration.underline,
|
||||
),
|
||||
),
|
||||
child: provider.isResending
|
||||
? SizedBox(
|
||||
width: 16,
|
||||
height: 16,
|
||||
child: CircularProgressIndicator(
|
||||
strokeWidth: 2,
|
||||
color: theme.colorScheme.primary,
|
||||
),
|
||||
)
|
||||
: Text(label),
|
||||
return ResendLink(
|
||||
label: label,
|
||||
onPressed: provider.resendCode,
|
||||
isDisabled: isDisabled,
|
||||
isLoading: provider.isResending,
|
||||
);
|
||||
}
|
||||
|
||||
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,5 +1,6 @@
|
||||
class SignupConfirmationArgs {
|
||||
final String? email;
|
||||
final String? password;
|
||||
|
||||
const SignupConfirmationArgs({this.email});
|
||||
const SignupConfirmationArgs({this.email, this.password});
|
||||
}
|
||||
|
||||
@@ -1,13 +1,16 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import 'package:pshared/provider/account.dart';
|
||||
import 'package:pweb/models/resend/action_result.dart';
|
||||
import 'package:pweb/models/resend/avaliability.dart';
|
||||
|
||||
import 'package:pweb/utils/snackbar.dart';
|
||||
import 'package:pweb/utils/error/snackbar.dart';
|
||||
import 'package:pweb/utils/cooldown_format.dart';
|
||||
import 'package:pweb/controllers/signup/confirmation_card.dart';
|
||||
import 'package:pweb/widgets/resend_link.dart';
|
||||
import 'package:pweb/widgets/vspacer.dart';
|
||||
|
||||
import 'package:pweb/generated/i18n/app_localizations.dart';
|
||||
@@ -15,7 +18,6 @@ import 'package:pweb/generated/i18n/app_localizations.dart';
|
||||
part 'state.dart';
|
||||
part 'content.dart';
|
||||
part 'badge.dart';
|
||||
part 'resend_button.dart';
|
||||
|
||||
|
||||
class SignupConfirmationCard extends StatefulWidget {
|
||||
|
||||
@@ -63,11 +63,11 @@ class _SignupConfirmationContent extends StatelessWidget {
|
||||
runSpacing: 12,
|
||||
alignment: WrapAlignment.center,
|
||||
children: [
|
||||
_SignupConfirmationResendButton(
|
||||
canResend: canResend,
|
||||
isResending: isResending,
|
||||
resendLabel: resendLabel,
|
||||
onResend: onResend,
|
||||
ResendLink(
|
||||
label: resendLabel,
|
||||
onPressed: onResend,
|
||||
isDisabled: !canResend,
|
||||
isLoading: isResending,
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
@@ -1,36 +0,0 @@
|
||||
part of 'card.dart';
|
||||
|
||||
|
||||
class _SignupConfirmationResendButton extends StatelessWidget {
|
||||
final bool canResend;
|
||||
final bool isResending;
|
||||
final String resendLabel;
|
||||
final VoidCallback onResend;
|
||||
|
||||
const _SignupConfirmationResendButton({
|
||||
required this.canResend,
|
||||
required this.isResending,
|
||||
required this.resendLabel,
|
||||
required this.onResend,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final theme = Theme.of(context);
|
||||
|
||||
return ElevatedButton.icon(
|
||||
onPressed: canResend ? onResend : null,
|
||||
icon: isResending
|
||||
? SizedBox(
|
||||
width: 16,
|
||||
height: 16,
|
||||
child: CircularProgressIndicator(
|
||||
strokeWidth: 2,
|
||||
color: theme.colorScheme.onPrimary,
|
||||
),
|
||||
)
|
||||
: const Icon(Icons.mark_email_read_outlined),
|
||||
label: Text(resendLabel),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -3,61 +3,45 @@ part of 'card.dart';
|
||||
|
||||
class _SignupConfirmationCardState extends State<SignupConfirmationCard> {
|
||||
static const int _defaultCooldownSeconds = 60;
|
||||
|
||||
Timer? _cooldownTimer;
|
||||
int _cooldownRemainingSeconds = 0;
|
||||
bool _isResending = false;
|
||||
late final SignupConfirmationCardController _controller;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_startCooldown(_defaultCooldownSeconds);
|
||||
_controller = SignupConfirmationCardController(
|
||||
accountProvider: context.read<AccountProvider>(),
|
||||
defaultCooldown: const Duration(seconds: _defaultCooldownSeconds),
|
||||
);
|
||||
_controller.initialize(email: widget.email);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_cooldownTimer?.cancel();
|
||||
_controller.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
bool get _isCooldownActive => _cooldownRemainingSeconds > 0;
|
||||
|
||||
void _startCooldown(int seconds) {
|
||||
_cooldownTimer?.cancel();
|
||||
if (seconds <= 0) {
|
||||
setState(() => _cooldownRemainingSeconds = 0);
|
||||
return;
|
||||
@override
|
||||
void didUpdateWidget(covariant SignupConfirmationCard oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
if (oldWidget.email != widget.email) {
|
||||
_controller.updateEmail(widget.email);
|
||||
}
|
||||
setState(() => _cooldownRemainingSeconds = seconds);
|
||||
_cooldownTimer = Timer.periodic(const Duration(seconds: 1), (timer) {
|
||||
if (!mounted) {
|
||||
timer.cancel();
|
||||
return;
|
||||
}
|
||||
if (_cooldownRemainingSeconds <= 1) {
|
||||
timer.cancel();
|
||||
setState(() => _cooldownRemainingSeconds = 0);
|
||||
return;
|
||||
}
|
||||
setState(() => _cooldownRemainingSeconds -= 1);
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> _resendVerificationEmail() async {
|
||||
final email = widget.email?.trim();
|
||||
final locs = AppLocalizations.of(context)!;
|
||||
if (email == null || email.isEmpty) {
|
||||
notifyUser(context, locs.errorEmailMissing);
|
||||
return;
|
||||
}
|
||||
if (_isResending || _isCooldownActive) return;
|
||||
|
||||
setState(() => _isResending = true);
|
||||
try {
|
||||
await context.read<AccountProvider>().resendVerificationEmail(email);
|
||||
final result = await _controller.resendVerificationEmail();
|
||||
if (!mounted) return;
|
||||
notifyUser(context, locs.signupConfirmationResent(email));
|
||||
_startCooldown(_defaultCooldownSeconds);
|
||||
if (result == ResendActionResult.missingEmail) {
|
||||
notifyUser(context, locs.errorEmailMissing);
|
||||
return;
|
||||
}
|
||||
if (result == ResendActionResult.sent) {
|
||||
final email = widget.email?.trim() ?? '';
|
||||
notifyUser(context, locs.signupConfirmationResent(email));
|
||||
}
|
||||
} catch (e) {
|
||||
if (!mounted) return;
|
||||
postNotifyUserOfErrorX(
|
||||
@@ -65,22 +49,9 @@ class _SignupConfirmationCardState extends State<SignupConfirmationCard> {
|
||||
errorSituation: locs.signupConfirmationResendError,
|
||||
exception: e,
|
||||
);
|
||||
} finally {
|
||||
if (mounted) {
|
||||
setState(() => _isResending = false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final theme = Theme.of(context);
|
||||
@@ -89,34 +60,44 @@ class _SignupConfirmationCardState extends State<SignupConfirmationCard> {
|
||||
final description = (email != null && email.isNotEmpty)
|
||||
? locs.signupConfirmationDescription(email)
|
||||
: locs.signupConfirmationDescriptionNoEmail;
|
||||
final canResend = !_isResending && !_isCooldownActive && email != null && email.isNotEmpty;
|
||||
final resendLabel = _isCooldownActive
|
||||
? locs.signupConfirmationResendCooldown(_formatCooldown(_cooldownRemainingSeconds))
|
||||
: locs.signupConfirmationResend;
|
||||
|
||||
final content = _SignupConfirmationContent(
|
||||
email: email,
|
||||
description: description,
|
||||
canResend: canResend,
|
||||
resendLabel: resendLabel,
|
||||
isResending: _isResending,
|
||||
onResend: _resendVerificationEmail,
|
||||
);
|
||||
return AnimatedBuilder(
|
||||
animation: _controller,
|
||||
builder: (context, _) {
|
||||
final availability = _controller.resendAvailability;
|
||||
final canResend = availability == ResendAvailability.available;
|
||||
final isResending = availability == ResendAvailability.resending;
|
||||
final resendLabel = availability == ResendAvailability.cooldown
|
||||
? locs.signupConfirmationResendCooldown(
|
||||
formatCooldownSeconds(_controller.cooldownRemainingSeconds),
|
||||
)
|
||||
: locs.signupConfirmationResend;
|
||||
|
||||
if (widget.isEmbedded) return content;
|
||||
final content = _SignupConfirmationContent(
|
||||
email: email,
|
||||
description: description,
|
||||
canResend: canResend,
|
||||
resendLabel: resendLabel,
|
||||
isResending: isResending,
|
||||
onResend: _resendVerificationEmail,
|
||||
);
|
||||
|
||||
return Card(
|
||||
elevation: 0,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(20),
|
||||
side: BorderSide(
|
||||
color: theme.dividerColor.withValues(alpha: 0.6),
|
||||
),
|
||||
),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(28),
|
||||
child: content,
|
||||
),
|
||||
if (widget.isEmbedded) return content;
|
||||
|
||||
return Card(
|
||||
elevation: 0,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(20),
|
||||
side: BorderSide(
|
||||
color: theme.dividerColor.withValues(alpha: 0.6),
|
||||
),
|
||||
),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(28),
|
||||
child: content,
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,76 +1,106 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import 'package:pshared/provider/account.dart';
|
||||
|
||||
import 'package:pweb/app/router/pages.dart';
|
||||
import 'package:pweb/pages/login/app_bar.dart';
|
||||
import 'package:pweb/pages/signup/confirmation/card/card.dart';
|
||||
import 'package:pweb/pages/signup/confirmation/login_prompt.dart';
|
||||
import 'package:pweb/controllers/signup/confirmation.dart';
|
||||
import 'package:pweb/pages/with_footer.dart';
|
||||
import 'package:pweb/widgets/vspacer.dart';
|
||||
|
||||
|
||||
class SignUpConfirmationPage extends StatefulWidget {
|
||||
final String? email;
|
||||
final String? password;
|
||||
|
||||
const SignUpConfirmationPage({super.key, this.email});
|
||||
const SignUpConfirmationPage({
|
||||
super.key,
|
||||
this.email,
|
||||
this.password,
|
||||
});
|
||||
|
||||
@override
|
||||
State<SignUpConfirmationPage> createState() => _SignUpConfirmationPageState();
|
||||
}
|
||||
|
||||
class _SignUpConfirmationPageState extends State<SignUpConfirmationPage> {
|
||||
late final SignupConfirmationController _controller;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_controller = SignupConfirmationController(
|
||||
accountProvider: context.read<AccountProvider>(),
|
||||
)..addListener(_handleAuthorizationStatus);
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) => _startPolling());
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_controller.removeListener(_handleAuthorizationStatus);
|
||||
_controller.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _startPolling() {
|
||||
if (!mounted) return;
|
||||
final email = widget.email?.trim();
|
||||
final password = widget.password;
|
||||
if (email == null || email.isEmpty || password == null || password.isEmpty) {
|
||||
return;
|
||||
}
|
||||
_controller.startPolling(
|
||||
email: email,
|
||||
password: password,
|
||||
locale: Localizations.localeOf(context).toLanguageTag(),
|
||||
);
|
||||
}
|
||||
|
||||
void _handleAuthorizationStatus() {
|
||||
if (!_controller.isAuthorized || !mounted) return;
|
||||
navigateAndReplace(context, Pages.login);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final email = widget.email?.trim();
|
||||
final width = MediaQuery.of(context).size.width;
|
||||
final isWide = width >= 980;
|
||||
|
||||
return PageWithFooter(
|
||||
appBar: const LoginAppBar(),
|
||||
child: LayoutBuilder(
|
||||
builder: (context, constraints) {
|
||||
final isWide = constraints.maxWidth >= 980;
|
||||
|
||||
return ListView(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 32),
|
||||
children: [
|
||||
Center(
|
||||
child: ConstrainedBox(
|
||||
constraints: BoxConstraints(maxWidth: isWide ? 980 : 720),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.center,
|
||||
children: [
|
||||
Card(
|
||||
elevation: 0,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(22),
|
||||
side: BorderSide(
|
||||
color: Theme.of(context).dividerColor.withValues(alpha: 0.6),
|
||||
),
|
||||
),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(28),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.center,
|
||||
children: [
|
||||
SignupConfirmationCard(
|
||||
email: email,
|
||||
isEmbedded: true,
|
||||
),
|
||||
const VSpacer(multiplier: 2),
|
||||
Divider(
|
||||
height: 1,
|
||||
color: Theme.of(context).dividerColor.withValues(alpha: 0.6),
|
||||
),
|
||||
const VSpacer(multiplier: 2),
|
||||
const SignupConfirmationLoginPrompt(isEmbedded: true),
|
||||
],
|
||||
),
|
||||
),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 32),
|
||||
child: Center(
|
||||
child: ConstrainedBox(
|
||||
constraints: BoxConstraints(maxWidth: isWide ? 980 : 720),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.center,
|
||||
children: [
|
||||
Card(
|
||||
elevation: 0,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(22),
|
||||
side: BorderSide(
|
||||
color: Theme.of(context).dividerColor.withValues(alpha: 0.6),
|
||||
),
|
||||
),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(28),
|
||||
child:
|
||||
SignupConfirmationCard(
|
||||
email: email,
|
||||
isEmbedded: true,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -95,6 +95,7 @@ class SignUpFormState extends State<SignUpForm> {
|
||||
Pages.signupConfirm.name,
|
||||
extra: SignupConfirmationArgs(
|
||||
email: controllers.email.text.trim(),
|
||||
password: controllers.password.text,
|
||||
),
|
||||
);
|
||||
},
|
||||
|
||||
@@ -12,6 +12,7 @@ import 'package:pweb/pages/verification/controller.dart';
|
||||
import 'package:pweb/pages/verification/resend_dialog.dart';
|
||||
import 'package:pweb/utils/snackbar.dart';
|
||||
import 'package:pweb/utils/error/snackbar.dart';
|
||||
import 'package:pweb/widgets/resend_link.dart';
|
||||
|
||||
import 'package:pweb/generated/i18n/app_localizations.dart';
|
||||
|
||||
@@ -64,6 +65,12 @@ class AccountVerificationContentState
|
||||
Widget content;
|
||||
if (controller.isLoading) {
|
||||
content = const Center(child: CircularProgressIndicator());
|
||||
} else if (controller.isAlreadyVerified) {
|
||||
content = StatusPageSuccess(
|
||||
successMessage: locs.accountAlreadyVerified,
|
||||
successDescription: locs.accountAlreadyVerifiedDescription,
|
||||
action: action,
|
||||
);
|
||||
} else if (controller.isSuccess) {
|
||||
content = StatusPageSuccess(
|
||||
successMessage: locs.accountVerified,
|
||||
@@ -78,18 +85,11 @@ class AccountVerificationContentState
|
||||
exception:
|
||||
controller.error ?? Exception(locs.accountVerificationFailed),
|
||||
action: controller.canResend
|
||||
? OutlinedButton.icon(
|
||||
onPressed: controller.isResending
|
||||
? null
|
||||
: _resendVerificationEmail,
|
||||
icon: controller.isResending
|
||||
? const SizedBox(
|
||||
width: 18,
|
||||
height: 18,
|
||||
child: CircularProgressIndicator(strokeWidth: 2),
|
||||
)
|
||||
: const Icon(Icons.mark_email_unread_outlined),
|
||||
label: Text(locs.signupConfirmationResend),
|
||||
? ResendLink(
|
||||
label: locs.signupConfirmationResend,
|
||||
onPressed: _resendVerificationEmail,
|
||||
isDisabled: !controller.canResend || controller.isResending,
|
||||
isLoading: controller.isResending,
|
||||
)
|
||||
: null,
|
||||
);
|
||||
|
||||
@@ -26,6 +26,7 @@ class AccountVerificationController extends ChangeNotifier {
|
||||
Exception? get error => _verificationProvider.error;
|
||||
bool get canResend => _verificationProvider.canResendVerification;
|
||||
bool get isResending => _resendStatus == FlowStatus.resending;
|
||||
bool get isAlreadyVerified => _verificationProvider.isTokenAlreadyUsed;
|
||||
|
||||
void startVerification(String token) {
|
||||
final trimmed = token.trim();
|
||||
|
||||
@@ -22,6 +22,7 @@ class TwoFactorProvider extends ChangeNotifier {
|
||||
String? _currentPendingToken;
|
||||
Timer? _cooldownTimer;
|
||||
int _cooldownRemainingSeconds = 0;
|
||||
DateTime? _cooldownUntil;
|
||||
|
||||
FlowStatus get status => _status;
|
||||
bool get isSubmitting => _status == FlowStatus.submitting;
|
||||
@@ -108,48 +109,69 @@ class TwoFactorProvider extends ChangeNotifier {
|
||||
return;
|
||||
}
|
||||
|
||||
final remaining = pending.cooldownRemainingSeconds;
|
||||
if (remaining <= 0) {
|
||||
final until = pending.cooldownUntil;
|
||||
if (until == null) {
|
||||
_stopCooldown(notify: _cooldownRemainingSeconds != 0);
|
||||
return;
|
||||
}
|
||||
|
||||
if (_cooldownRemainingSeconds != remaining) {
|
||||
_startCooldown(remaining);
|
||||
if (!_isCooldownActive(until) && _cooldownRemainingSeconds != 0) {
|
||||
_stopCooldown(notify: true);
|
||||
return;
|
||||
}
|
||||
|
||||
if (_cooldownUntil == null || _cooldownUntil != until) {
|
||||
_startCooldownUntil(until);
|
||||
}
|
||||
}
|
||||
|
||||
void _startCooldown(int seconds) {
|
||||
final until = DateTime.now().add(Duration(seconds: seconds));
|
||||
_startCooldownUntil(until);
|
||||
}
|
||||
|
||||
void _startCooldownUntil(DateTime until) {
|
||||
_cooldownTimer?.cancel();
|
||||
_cooldownRemainingSeconds = seconds;
|
||||
_cooldownUntil = until;
|
||||
_cooldownRemainingSeconds = _cooldownRemaining();
|
||||
|
||||
if (_cooldownRemainingSeconds <= 0) {
|
||||
_cooldownTimer = null;
|
||||
_cooldownUntil = null;
|
||||
notifyListeners();
|
||||
return;
|
||||
}
|
||||
|
||||
_cooldownTimer = Timer.periodic(const Duration(seconds: 1), (timer) {
|
||||
if (_cooldownRemainingSeconds <= 1) {
|
||||
_cooldownRemainingSeconds = 0;
|
||||
_cooldownTimer?.cancel();
|
||||
_cooldownTimer = null;
|
||||
notifyListeners();
|
||||
final remaining = _cooldownRemaining();
|
||||
if (remaining <= 0) {
|
||||
_stopCooldown(notify: true);
|
||||
return;
|
||||
}
|
||||
|
||||
_cooldownRemainingSeconds -= 1;
|
||||
notifyListeners();
|
||||
if (remaining != _cooldownRemainingSeconds) {
|
||||
_cooldownRemainingSeconds = remaining;
|
||||
notifyListeners();
|
||||
}
|
||||
});
|
||||
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
bool _isCooldownActive(DateTime until) => until.isAfter(DateTime.now());
|
||||
|
||||
int _cooldownRemaining() {
|
||||
final until = _cooldownUntil;
|
||||
if (until == null) return 0;
|
||||
final remaining = until.difference(DateTime.now()).inSeconds;
|
||||
return remaining < 0 ? 0 : remaining;
|
||||
}
|
||||
|
||||
void _stopCooldown({bool notify = false}) {
|
||||
_cooldownTimer?.cancel();
|
||||
_cooldownTimer = null;
|
||||
final hadCooldown = _cooldownRemainingSeconds != 0;
|
||||
_cooldownRemainingSeconds = 0;
|
||||
_cooldownUntil = null;
|
||||
|
||||
if (notify && hadCooldown) {
|
||||
notifyListeners();
|
||||
|
||||
8
frontend/pweb/lib/utils/cooldown_format.dart
Normal file
8
frontend/pweb/lib/utils/cooldown_format.dart
Normal file
@@ -0,0 +1,8 @@
|
||||
String formatCooldownSeconds(int seconds) {
|
||||
final minutes = seconds ~/ 60;
|
||||
final remainingSeconds = seconds % 60;
|
||||
if (minutes > 0) {
|
||||
return '$minutes:${remainingSeconds.toString().padLeft(2, '0')}';
|
||||
}
|
||||
return remainingSeconds.toString();
|
||||
}
|
||||
@@ -15,6 +15,7 @@ class ErrorHandler {
|
||||
'account_not_verified': locs.errorAccountNotVerified,
|
||||
'unauthorized': locs.errorLoginUnauthorized,
|
||||
'verification_token_not_found': locs.errorVerificationTokenNotFound,
|
||||
'user_already_registered': locs.errorDuplicateEmail,
|
||||
'internal_error': locs.errorInternalError,
|
||||
'invalid_target': locs.errorInvalidTarget,
|
||||
'pending_token_required': locs.errorPendingTokenRequired,
|
||||
@@ -62,6 +63,9 @@ class ErrorHandler {
|
||||
}
|
||||
|
||||
static String _handleErrorResponseLocs(AppLocalizations locs, ErrorResponse e) {
|
||||
if (e.source == 'user_already_registered') {
|
||||
return locs.errorDuplicateEmail;
|
||||
}
|
||||
final errorMessages = getErrorMessagesLocs(locs);
|
||||
// Return the localized message if we recognize the error key, else use the raw details
|
||||
return errorMessages[e.error] ?? e.details;
|
||||
|
||||
48
frontend/pweb/lib/widgets/resend_link.dart
Normal file
48
frontend/pweb/lib/widgets/resend_link.dart
Normal file
@@ -0,0 +1,48 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
|
||||
class ResendLink extends StatelessWidget {
|
||||
final String label;
|
||||
final VoidCallback onPressed;
|
||||
final bool isDisabled;
|
||||
final bool isLoading;
|
||||
|
||||
const ResendLink({
|
||||
super.key,
|
||||
required this.label,
|
||||
required this.onPressed,
|
||||
this.isDisabled = false,
|
||||
this.isLoading = false,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final theme = Theme.of(context);
|
||||
final color = theme.colorScheme.primary;
|
||||
final isButtonDisabled = isDisabled || isLoading;
|
||||
|
||||
return TextButton(
|
||||
onPressed: isButtonDisabled ? null : onPressed,
|
||||
style: TextButton.styleFrom(
|
||||
padding: EdgeInsets.zero,
|
||||
minimumSize: const Size(0, 0),
|
||||
tapTargetSize: MaterialTapTargetSize.shrinkWrap,
|
||||
alignment: Alignment.centerLeft,
|
||||
foregroundColor: color,
|
||||
textStyle: theme.textTheme.bodyMedium?.copyWith(
|
||||
decoration: TextDecoration.underline,
|
||||
),
|
||||
),
|
||||
child: isLoading
|
||||
? SizedBox(
|
||||
width: 16,
|
||||
height: 16,
|
||||
child: CircularProgressIndicator(
|
||||
strokeWidth: 2,
|
||||
color: color,
|
||||
),
|
||||
)
|
||||
: Text(label),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user