@@ -1,8 +1,4 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Blazored.LocalStorage;
|
||||
using Blazored.LocalStorage;
|
||||
using FrontOffice.BFF.User.Protobuf.Protos.User;
|
||||
using FrontOffice.BFF.User.Protobuf.Validator;
|
||||
using FrontOffice.Main.Utilities;
|
||||
@@ -20,18 +16,13 @@ public partial class Phone : IDisposable
|
||||
private const string TokenStorageKey = "auth:token";
|
||||
private const string OtpPurpose = "Login";
|
||||
|
||||
private readonly CreateNewOtpTokenRequestValidator _requestValidator = new();
|
||||
private readonly CreateNewOtpTokenRequest _request = new();
|
||||
|
||||
private CreateNewOtpTokenRequestValidator _requestValidator = new();
|
||||
private CreateNewOtpTokenRequest _request = new();
|
||||
private MudForm? _form;
|
||||
|
||||
private bool _isBusy;
|
||||
private bool _acceptTerms;
|
||||
private string? _errorMessage;
|
||||
private string? _infoMessage;
|
||||
private string? _redirect;
|
||||
private int? _remainingAttempts;
|
||||
private int _cooldownSeconds;
|
||||
private Timer? _cooldownTimer;
|
||||
private CancellationTokenSource? _sendCts;
|
||||
|
||||
[Inject] private ILocalStorageService LocalStorage { get; set; } = default!;
|
||||
@@ -66,18 +57,12 @@ public partial class Phone : IDisposable
|
||||
private async Task SendOtpAsync()
|
||||
{
|
||||
_errorMessage = null;
|
||||
_infoMessage = null;
|
||||
|
||||
if (_form is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
await _form.Validate();
|
||||
if (!_form.IsValid)
|
||||
{
|
||||
return;
|
||||
}
|
||||
return;
|
||||
|
||||
_isBusy = true;
|
||||
_sendCts?.Cancel();
|
||||
@@ -94,21 +79,24 @@ public partial class Phone : IDisposable
|
||||
}
|
||||
|
||||
var metadata = await BuildAuthMetadataAsync();
|
||||
var response = metadata is not null
|
||||
? await UserClient.CreateNewOtpTokenAsync(_request, metadata, cancellationToken: _sendCts.Token)
|
||||
: await UserClient.CreateNewOtpTokenAsync(_request, cancellationToken: _sendCts.Token);
|
||||
CreateNewOtpTokenResponse response;
|
||||
if (metadata is not null)
|
||||
{
|
||||
response = await UserClient.CreateNewOtpTokenAsync(_request, metadata, cancellationToken: _sendCts.Token);
|
||||
}
|
||||
else
|
||||
{
|
||||
response = await UserClient.CreateNewOtpTokenAsync(_request, cancellationToken: _sendCts.Token);
|
||||
}
|
||||
|
||||
if (response?.Success != true)
|
||||
{
|
||||
_errorMessage = !string.IsNullOrWhiteSpace(response?.Message)
|
||||
? response!.Message
|
||||
: "ارسال رمز پویا با خطا روبهرو شد. لطفاً دوباره تلاش کنید.";
|
||||
ApplyServerState(response, false);
|
||||
_errorMessage = string.IsNullOrWhiteSpace(response?.Message)
|
||||
? "ارسال رمز پویا با خطا مواجه شد. لطفاً دوباره تلاش کنید."
|
||||
: response!.Message;
|
||||
return;
|
||||
}
|
||||
|
||||
ApplyServerState(response, true);
|
||||
|
||||
await LocalStorage.SetItemAsync(PhoneStorageKey, _request.Mobile);
|
||||
if (!string.IsNullOrWhiteSpace(_redirect))
|
||||
{
|
||||
@@ -131,7 +119,7 @@ public partial class Phone : IDisposable
|
||||
{
|
||||
_errorMessage = !string.IsNullOrWhiteSpace(rpcEx.Status.Detail)
|
||||
? rpcEx.Status.Detail
|
||||
: "ارسال رمز پویا با خطا روبهرو شد. لطفاً دوباره تلاش کنید.";
|
||||
: "ارسال رمز پویا با خطا مواجه شد. لطفاً دوباره تلاش کنید.";
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
@@ -149,54 +137,6 @@ public partial class Phone : IDisposable
|
||||
}
|
||||
}
|
||||
|
||||
private void ApplyServerState(CreateNewOtpTokenResponse? response, bool isSuccess)
|
||||
{
|
||||
if (response is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_infoMessage = isSuccess
|
||||
? (string.IsNullOrWhiteSpace(response.Message) ? "رمز پویا ارسال شد." : response.Message)
|
||||
: null;
|
||||
|
||||
if (response.RemainingAttempts >= 0)
|
||||
{
|
||||
_remainingAttempts = response.RemainingAttempts;
|
||||
}
|
||||
|
||||
if (response.RemainingSeconds > 0)
|
||||
{
|
||||
StartCooldown(response.RemainingSeconds);
|
||||
}
|
||||
else if (isSuccess)
|
||||
{
|
||||
StopCooldown();
|
||||
}
|
||||
}
|
||||
|
||||
private void StartCooldown(int seconds)
|
||||
{
|
||||
StopCooldown();
|
||||
_cooldownSeconds = seconds;
|
||||
_cooldownTimer = new Timer(_ =>
|
||||
{
|
||||
var remaining = Interlocked.Decrement(ref _cooldownSeconds);
|
||||
if (remaining <= 0)
|
||||
{
|
||||
StopCooldown();
|
||||
}
|
||||
_ = InvokeAsync(StateHasChanged);
|
||||
}, null, 1000, 1000);
|
||||
}
|
||||
|
||||
private void StopCooldown()
|
||||
{
|
||||
_cooldownTimer?.Dispose();
|
||||
_cooldownTimer = null;
|
||||
_cooldownSeconds = 0;
|
||||
}
|
||||
|
||||
private async Task<Metadata?> BuildAuthMetadataAsync()
|
||||
{
|
||||
var token = await LocalStorage.GetItemAsync<string>(TokenStorageKey);
|
||||
@@ -215,6 +155,6 @@ public partial class Phone : IDisposable
|
||||
{
|
||||
_sendCts?.Cancel();
|
||||
_sendCts?.Dispose();
|
||||
StopCooldown();
|
||||
_sendCts = null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user