Implemented cooldown before User is able to resend confirmation code for 2fa
This commit is contained in:
@@ -10,6 +10,7 @@ import 'package:pshared/api/requests/signup.dart';
|
||||
import 'package:pshared/api/requests/login_data.dart';
|
||||
import 'package:pshared/config/constants.dart';
|
||||
import 'package:pshared/models/account/account.dart';
|
||||
import 'package:pshared/api/responses/confirmation.dart';
|
||||
import 'package:pshared/models/auth/login_outcome.dart';
|
||||
import 'package:pshared/models/auth/pending_login.dart';
|
||||
import 'package:pshared/models/describable.dart';
|
||||
@@ -101,8 +102,8 @@ class AccountProvider extends ChangeNotifier {
|
||||
if (pending == null) {
|
||||
throw Exception('Pending login data is missing');
|
||||
}
|
||||
await VerificationService.requestLoginCode(pending);
|
||||
_pendingLogin = pending;
|
||||
final confirmation = await VerificationService.requestLoginCode(pending);
|
||||
_pendingLogin = _applyConfirmationMeta(pending, confirmation);
|
||||
_authState = AuthState.idle;
|
||||
_setResource(_resource.copyWith(isLoading: false));
|
||||
}
|
||||
@@ -114,6 +115,27 @@ class AccountProvider extends ChangeNotifier {
|
||||
}
|
||||
}
|
||||
|
||||
PendingLogin _applyConfirmationMeta(PendingLogin pending, ConfirmationResponse confirmation) {
|
||||
final ttlSeconds = confirmation.ttlSeconds != 0 ? confirmation.ttlSeconds : pending.ttlSeconds;
|
||||
final destination = confirmation.destination.isNotEmpty ? confirmation.destination : pending.destination;
|
||||
final cooldownSeconds = confirmation.cooldownSeconds;
|
||||
|
||||
return pending.copyWith(
|
||||
ttlSeconds: ttlSeconds,
|
||||
destination: destination,
|
||||
cooldownSeconds: cooldownSeconds > 0 ? cooldownSeconds : null,
|
||||
cooldownUntil: cooldownSeconds > 0 ? DateTime.now().add(confirmation.cooldownDuration) : null,
|
||||
clearCooldown: cooldownSeconds <= 0,
|
||||
);
|
||||
}
|
||||
|
||||
void updatePendingLogin(ConfirmationResponse confirmation) {
|
||||
final pending = _pendingLogin;
|
||||
if (pending == null) return;
|
||||
_pendingLogin = _applyConfirmationMeta(pending, confirmation);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void completePendingLogin(Account account) {
|
||||
_pendingLogin = null;
|
||||
_authState = AuthState.ready;
|
||||
|
||||
Reference in New Issue
Block a user