This commit is contained in:
MeysamMoghaddam
2025-09-28 08:38:13 +03:30
parent 63338dbb79
commit d2cd305581
4 changed files with 267 additions and 150 deletions

View File

@@ -1,4 +1,8 @@
using Blazored.LocalStorage;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Blazored.LocalStorage;
using FrontOffice.BFF.User.Protobuf.Protos.User;
using FrontOffice.BFF.User.Protobuf.Validator;
using FrontOffice.Main.Utilities;
@@ -16,13 +20,18 @@ public partial class Phone : IDisposable
private const string TokenStorageKey = "auth:token";
private const string OtpPurpose = "Login";
private CreateNewOtpTokenRequestValidator _requestValidator = new();
private CreateNewOtpTokenRequest _request = new();
private MudForm? _form;
private readonly CreateNewOtpTokenRequestValidator _requestValidator = new();
private readonly 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!;
@@ -57,12 +66,18 @@ 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();
@@ -79,24 +94,21 @@ public partial class Phone : IDisposable
}
var metadata = await BuildAuthMetadataAsync();
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);
}
var response = metadata is not null
? await UserClient.CreateNewOtpTokenAsync(_request, metadata, cancellationToken: _sendCts.Token)
: await UserClient.CreateNewOtpTokenAsync(_request, cancellationToken: _sendCts.Token);
if (response?.Success != true)
{
_errorMessage = string.IsNullOrWhiteSpace(response?.Message)
? "ارسال رمز پویا با خطا مواجه شد. لطفاً دوباره تلاش کنید."
: response!.Message;
_errorMessage = !string.IsNullOrWhiteSpace(response?.Message)
? response!.Message
: "ارسال رمز پویا با خطا روبه‌رو شد. لطفاً دوباره تلاش کنید.";
ApplyServerState(response, false);
return;
}
ApplyServerState(response, true);
await LocalStorage.SetItemAsync(PhoneStorageKey, _request.Mobile);
if (!string.IsNullOrWhiteSpace(_redirect))
{
@@ -119,7 +131,7 @@ public partial class Phone : IDisposable
{
_errorMessage = !string.IsNullOrWhiteSpace(rpcEx.Status.Detail)
? rpcEx.Status.Detail
: "ارسال رمز پویا با خطا مواجه شد. لطفاً دوباره تلاش کنید.";
: "ارسال رمز پویا با خطا روبه‌رو شد. لطفاً دوباره تلاش کنید.";
}
catch (OperationCanceledException)
{
@@ -137,6 +149,54 @@ 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);
@@ -155,6 +215,6 @@ public partial class Phone : IDisposable
{
_sendCts?.Cancel();
_sendCts?.Dispose();
_sendCts = null;
StopCooldown();
}
}