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

@@ -6,7 +6,7 @@
<MudCard Class="pa-6" Style="max-width:420px;width:100%;"> <MudCard Class="pa-6" Style="max-width:420px;width:100%;">
<MudCardContent> <MudCardContent>
<MudText Typo="Typo.h5" Class="mb-1" Align="Align.Center">ورود به حساب کاربری</MudText> <MudText Typo="Typo.h5" Class="mb-1" Align="Align.Center">ورود به حساب کاربری</MudText>
<MudText Typo="Typo.body2" Class="mb-4" Align="Align.Center">لطفاً شماره موبایل خود را وارد کنید تا رمز پویا ارسال شود.</MudText> <MudText Typo="Typo.body2" Class="mb-4" Align="Align.Center">لطفاً شماره موبایل خود را وارد کنید تا رمز پویا برایتان ارسال شود.</MudText>
<MudForm @ref="_form" Model="_request" Validation="@(_requestValidator.ValidateValue)"> <MudForm @ref="_form" Model="_request" Validation="@(_requestValidator.ValidateValue)">
<MudTextField @bind-Value="_request.Mobile" <MudTextField @bind-Value="_request.Mobile"
@@ -17,13 +17,33 @@
Immediate="true" Immediate="true"
Required="true" Required="true"
RequiredError="وارد کردن شماره موبایل الزامی است." RequiredError="وارد کردن شماره موبایل الزامی است."
HelperText="مثال: 09121234567" HelperText="مثال: 09123456789"
Class="mb-2" /> Class="mb-2" />
<MudCheckBox T="bool" <MudCheckBox T="bool"
@bind-Checked="_acceptTerms"
Label="شرایط و قوانین را می‌پذیرم" Label="شرایط و قوانین را می‌پذیرم"
Class="mb-2" /> Class="mb-2" />
@if (_remainingAttempts.HasValue)
{
<MudText Typo="Typo.caption" Align="Align.Center" Class="mb-1">
تعداد تلاش‌های باقی‌مانده: @_remainingAttempts
</MudText>
}
@if (_cooldownSeconds > 0)
{
<MudText Typo="Typo.caption" Align="Align.Center" Class="mb-1">
امکان درخواست مجدد تا @_cooldownSeconds ثانیه دیگر فعال می‌شود.
</MudText>
}
@if (!string.IsNullOrWhiteSpace(_infoMessage))
{
<MudAlert Severity="Severity.Success" Dense="true" Elevation="0" Class="mb-2">@_infoMessage</MudAlert>
}
@if (!string.IsNullOrWhiteSpace(_errorMessage)) @if (!string.IsNullOrWhiteSpace(_errorMessage))
{ {
<MudAlert Severity="Severity.Error" Dense="true" Elevation="0" Class="mb-2">@_errorMessage</MudAlert> <MudAlert Severity="Severity.Error" Dense="true" Elevation="0" Class="mb-2">@_errorMessage</MudAlert>
@@ -32,7 +52,8 @@
<MudButton Variant="Variant.Filled" <MudButton Variant="Variant.Filled"
Color="Color.Primary" Color="Color.Primary"
OnClick="SendOtpAsync" OnClick="SendOtpAsync"
Disabled="_isBusy" Disabled="_isBusy || !_acceptTerms || _cooldownSeconds > 0"
Loading="_isBusy"
FullWidth="true" FullWidth="true"
Class="mt-2"> Class="mt-2">
ارسال رمز پویا ارسال رمز پویا

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.Protos.User;
using FrontOffice.BFF.User.Protobuf.Validator; using FrontOffice.BFF.User.Protobuf.Validator;
using FrontOffice.Main.Utilities; using FrontOffice.Main.Utilities;
@@ -16,13 +20,18 @@ public partial class Phone : IDisposable
private const string TokenStorageKey = "auth:token"; private const string TokenStorageKey = "auth:token";
private const string OtpPurpose = "Login"; private const string OtpPurpose = "Login";
private CreateNewOtpTokenRequestValidator _requestValidator = new(); private readonly CreateNewOtpTokenRequestValidator _requestValidator = new();
private CreateNewOtpTokenRequest _request = new(); private readonly CreateNewOtpTokenRequest _request = new();
private MudForm? _form;
private MudForm? _form;
private bool _isBusy; private bool _isBusy;
private bool _acceptTerms;
private string? _errorMessage; private string? _errorMessage;
private string? _infoMessage;
private string? _redirect; private string? _redirect;
private int? _remainingAttempts;
private int _cooldownSeconds;
private Timer? _cooldownTimer;
private CancellationTokenSource? _sendCts; private CancellationTokenSource? _sendCts;
[Inject] private ILocalStorageService LocalStorage { get; set; } = default!; [Inject] private ILocalStorageService LocalStorage { get; set; } = default!;
@@ -57,12 +66,18 @@ public partial class Phone : IDisposable
private async Task SendOtpAsync() private async Task SendOtpAsync()
{ {
_errorMessage = null; _errorMessage = null;
_infoMessage = null;
if (_form is null) if (_form is null)
{
return; return;
}
await _form.Validate(); await _form.Validate();
if (!_form.IsValid) if (!_form.IsValid)
{
return; return;
}
_isBusy = true; _isBusy = true;
_sendCts?.Cancel(); _sendCts?.Cancel();
@@ -79,24 +94,21 @@ public partial class Phone : IDisposable
} }
var metadata = await BuildAuthMetadataAsync(); var metadata = await BuildAuthMetadataAsync();
CreateNewOtpTokenResponse response; var response = metadata is not null
if (metadata is not null) ? await UserClient.CreateNewOtpTokenAsync(_request, metadata, cancellationToken: _sendCts.Token)
{ : await UserClient.CreateNewOtpTokenAsync(_request, cancellationToken: _sendCts.Token);
response = await UserClient.CreateNewOtpTokenAsync(_request, metadata, cancellationToken: _sendCts.Token);
}
else
{
response = await UserClient.CreateNewOtpTokenAsync(_request, cancellationToken: _sendCts.Token);
}
if (response?.Success != true) if (response?.Success != true)
{ {
_errorMessage = string.IsNullOrWhiteSpace(response?.Message) _errorMessage = !string.IsNullOrWhiteSpace(response?.Message)
? "ارسال رمز پویا با خطا مواجه شد. لطفاً دوباره تلاش کنید." ? response!.Message
: response!.Message; : "ارسال رمز پویا با خطا روبه‌رو شد. لطفاً دوباره تلاش کنید.";
ApplyServerState(response, false);
return; return;
} }
ApplyServerState(response, true);
await LocalStorage.SetItemAsync(PhoneStorageKey, _request.Mobile); await LocalStorage.SetItemAsync(PhoneStorageKey, _request.Mobile);
if (!string.IsNullOrWhiteSpace(_redirect)) if (!string.IsNullOrWhiteSpace(_redirect))
{ {
@@ -119,7 +131,7 @@ public partial class Phone : IDisposable
{ {
_errorMessage = !string.IsNullOrWhiteSpace(rpcEx.Status.Detail) _errorMessage = !string.IsNullOrWhiteSpace(rpcEx.Status.Detail)
? rpcEx.Status.Detail ? rpcEx.Status.Detail
: "ارسال رمز پویا با خطا مواجه شد. لطفاً دوباره تلاش کنید."; : "ارسال رمز پویا با خطا روبه‌رو شد. لطفاً دوباره تلاش کنید.";
} }
catch (OperationCanceledException) 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() private async Task<Metadata?> BuildAuthMetadataAsync()
{ {
var token = await LocalStorage.GetItemAsync<string>(TokenStorageKey); var token = await LocalStorage.GetItemAsync<string>(TokenStorageKey);
@@ -155,6 +215,6 @@ public partial class Phone : IDisposable
{ {
_sendCts?.Cancel(); _sendCts?.Cancel();
_sendCts?.Dispose(); _sendCts?.Dispose();
_sendCts = null; StopCooldown();
} }
} }

View File

@@ -5,14 +5,14 @@
<MudStack AlignItems="AlignItems.Center"> <MudStack AlignItems="AlignItems.Center">
<MudCard Class="pa-6" Style="max-width:420px;width:100%;"> <MudCard Class="pa-6" Style="max-width:420px;width:100%;">
<MudCardContent> <MudCardContent>
<MudText Typo="Typo.h5" Class="mb-1" Align="Align.Center">تأیید رمز پویا</MudText> <MudText Typo="Typo.h5" Class="mb-1" Align="Align.Center">کد ارسال‌شده را وارد کنید</MudText>
@if (!string.IsNullOrWhiteSpace(_phoneNumber)) @if (!string.IsNullOrWhiteSpace(_phoneNumber))
{ {
<MudText Typo="Typo.body2" Class="mb-4" Align="Align.Center">کد ارسال‌شده به @_phoneNumber را وارد کنید.</MudText> <MudText Typo="Typo.body2" Class="mb-4" Align="Align.Center">کد ارسال‌شده به @_phoneNumber را وارد نمایید.</MudText>
} }
else else
{ {
<MudText Typo="Typo.body2" Class="mb-4" Align="Align.Center">کد ارسال‌شده را وارد کنید.</MudText> <MudText Typo="Typo.body2" Class="mb-4" Align="Align.Center">کد پیامک شده را وارد نمایید.</MudText>
} }
<MudForm @ref="_form" Model="_request" Validation="@(_requestValidator.ValidateValue)"> <MudForm @ref="_form" Model="_request" Validation="@(_requestValidator.ValidateValue)">
@@ -24,17 +24,19 @@
Immediate="true" Immediate="true"
Required="true" Required="true"
RequiredError="وارد کردن رمز پویا الزامی است." RequiredError="وارد کردن رمز پویا الزامی است."
HelperText="کد ۶ رقمی" HelperText="کد ۵ یا ۶ رقمی"
Class="mb-2" Class="mb-2"
MaxLength="6" /> MaxLength="6" />
<MudText Typo="Typo.body1" Align="Align.Center" Class="mb-2"> <MudText Typo="Typo.caption" Align="Align.Center" Class="mb-1">
تلاش باقی‌مانده: @_attemptsLeft از @MaxVerificationAttempts تلاش‌های باقی‌مانده: @_attemptsLeft
</MudText> </MudText>
@if (!string.IsNullOrWhiteSpace(_errorMessage)) @if (_resendSeconds > 0)
{ {
<MudAlert Severity="Severity.Error" Dense="true" Elevation="0" Class="mb-2">@_errorMessage</MudAlert> <MudText Typo="Typo.caption" Align="Align.Center" Class="mb-1">
امکان ارسال مجدد تا @_resendSeconds ثانیه دیگر فعال می‌شود.
</MudText>
} }
@if (!string.IsNullOrWhiteSpace(_infoMessage)) @if (!string.IsNullOrWhiteSpace(_infoMessage))
@@ -42,13 +44,19 @@
<MudAlert Severity="Severity.Success" Dense="true" Elevation="0" Class="mb-2">@_infoMessage</MudAlert> <MudAlert Severity="Severity.Success" Dense="true" Elevation="0" Class="mb-2">@_infoMessage</MudAlert>
} }
@if (!string.IsNullOrWhiteSpace(_errorMessage))
{
<MudAlert Severity="Severity.Error" Dense="true" Elevation="0" Class="mb-2">@_errorMessage</MudAlert>
}
<MudStack Spacing="2" Class="mt-2"> <MudStack Spacing="2" Class="mt-2">
<MudButton Variant="Variant.Filled" <MudButton Variant="Variant.Filled"
Color="Color.Primary" Color="Color.Primary"
OnClick="VerifyOtpAsync" OnClick="VerifyOtpAsync"
Disabled="_isBusy || IsVerificationLocked" Disabled="_isBusy || IsVerificationLocked"
Loading="_isBusy"
FullWidth="true"> FullWidth="true">
تأیید و ورود تأیید و ادامه
</MudButton> </MudButton>
<MudButton Variant="Variant.Text" <MudButton Variant="Variant.Text"
Color="Color.Secondary" Color="Color.Secondary"
@@ -60,25 +68,13 @@
<MudDivider Class="my-2" /> <MudDivider Class="my-2" />
@if (_resendRemaining > 0) <MudButton Variant="Variant.Text"
{ Color="Color.Primary"
<MudText Typo="Typo.body1" Align="Align.Center"> Disabled="_isBusy || _resendSeconds > 0"
امکان ارسال مجدد تا @_resendRemaining ثانیه دیگر OnClick="ResendOtpAsync">
</MudText> ارسال دوباره رمز
} </MudButton>
else
{
<MudButton Variant="Variant.Text"
Color="Color.Primary"
Disabled="_isBusy"
OnClick="ResendOtpAsync">
ارسال مجدد رمز پویا
</MudButton>
}
</MudForm> </MudForm>
</MudCardContent> </MudCardContent>
</MudCard> </MudCard>
</MudStack> </MudStack>

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.Protos.User;
using FrontOffice.BFF.User.Protobuf.Validator; using FrontOffice.BFF.User.Protobuf.Validator;
using FrontOffice.Main.Utilities; using FrontOffice.Main.Utilities;
@@ -18,17 +22,17 @@ public partial class Verify : IDisposable
private const string RedirectStorageKey = "auth:redirect"; private const string RedirectStorageKey = "auth:redirect";
private const string TokenStorageKey = "auth:token"; private const string TokenStorageKey = "auth:token";
private VerifyOtpTokenRequestValidator _requestValidator = new(); private readonly VerifyOtpTokenRequestValidator _requestValidator = new();
private VerifyOtpTokenRequest _request = new(); private readonly VerifyOtpTokenRequest _request = new();
private MudForm? _form;
private MudForm? _form;
private bool _isBusy; private bool _isBusy;
private string? _phoneNumber; private string? _phoneNumber;
private string? _redirect; private string? _redirect;
private string? _errorMessage; private string? _errorMessage;
private string? _infoMessage; private string? _infoMessage;
private Timer? _resendTimer; private Timer? _resendTimer;
private int _resendRemaining; private int _resendSeconds;
private int _attemptsLeft = MaxVerificationAttempts; private int _attemptsLeft = MaxVerificationAttempts;
private CancellationTokenSource? _operationCts; private CancellationTokenSource? _operationCts;
@@ -75,7 +79,7 @@ public partial class Verify : IDisposable
} }
} }
StartResendCountdown(); ResetResendCountdown();
} }
private async Task VerifyOtpAsync() private async Task VerifyOtpAsync()
@@ -84,15 +88,19 @@ public partial class Verify : IDisposable
_infoMessage = null; _infoMessage = null;
if (_form is null) if (_form is null)
{
return; return;
}
await _form.Validate(); await _form.Validate();
if (!_form.IsValid) if (!_form.IsValid)
{
return; return;
}
if (IsVerificationLocked) if (IsVerificationLocked)
{ {
_errorMessage = "تعداد تلاش‌های مجاز به پایان رسیده است. لطفاً رمز جدید دریافت کنید."; _errorMessage = "تعداد تلاش‌های مجاز شما به پایان رسیده است. لطفاً رمز جدید دریافت کنید.";
return; return;
} }
@@ -117,15 +125,9 @@ public partial class Verify : IDisposable
} }
var metadata = await BuildAuthMetadataAsync(); var metadata = await BuildAuthMetadataAsync();
VerifyOtpTokenResponse response; var response = metadata is not null
if (metadata is not null) ? await UserClient.VerifyOtpTokenAsync(_request, metadata, cancellationToken: cancellationToken)
{ : await UserClient.VerifyOtpTokenAsync(_request, cancellationToken: cancellationToken);
response = await UserClient.VerifyOtpTokenAsync(_request, metadata, cancellationToken: cancellationToken);
}
else
{
response = await UserClient.VerifyOtpTokenAsync(_request, cancellationToken: cancellationToken);
}
if (response is null) if (response is null)
{ {
@@ -133,36 +135,21 @@ public partial class Verify : IDisposable
return; return;
} }
ApplyVerificationState(response);
if (response.Success) if (response.Success)
{ {
if (!string.IsNullOrWhiteSpace(response.Token)) await PersistTokenAsync(response.Token);
{
await LocalStorage.SetItemAsync(TokenStorageKey, response.Token);
}
else
{
await LocalStorage.RemoveItemAsync(TokenStorageKey);
}
await LocalStorage.RemoveItemAsync(PhoneStorageKey); await LocalStorage.RemoveItemAsync(PhoneStorageKey);
await LocalStorage.RemoveItemAsync(RedirectStorageKey); await LocalStorage.RemoveItemAsync(RedirectStorageKey);
_attemptsLeft = MaxVerificationAttempts;
_request.Code = string.Empty;
var target = !string.IsNullOrWhiteSpace(_redirect) ? _redirect : RouteConstants.Main.MainPage; var target = !string.IsNullOrWhiteSpace(_redirect) ? _redirect : RouteConstants.Main.MainPage;
if (!string.IsNullOrWhiteSpace(target)) Navigation.NavigateTo(target ?? RouteConstants.Main.MainPage, forceLoad: true);
{
Navigation.NavigateTo(target, forceLoad: true);
}
return;
} }
RegisterFailedAttempt(string.IsNullOrWhiteSpace(response.Message) ? "کد نادرست است." : response.Message);
} }
catch (RpcException rpcEx) catch (RpcException rpcEx)
{ {
await HandleVerificationFailureAsync(rpcEx); await HandleVerifyRpcFailureAsync(rpcEx);
} }
catch (OperationCanceledException) catch (OperationCanceledException)
{ {
@@ -179,19 +166,58 @@ public partial class Verify : IDisposable
} }
} }
private async Task HandleVerificationFailureAsync(RpcException rpcEx) private void ApplyVerificationState(VerifyOtpTokenResponse response)
{
if (response.RemainingSeconds > 0)
{
StartResendCountdown(response.RemainingSeconds);
}
else
{
ResetResendCountdown();
}
if (response.RemainingAttempts >= 0)
{
_attemptsLeft = response.RemainingAttempts;
}
if (response.Success)
{
_infoMessage = string.IsNullOrWhiteSpace(response.Message)
? "ورود با موفقیت انجام شد."
: response.Message;
}
else
{
var baseMessage = string.IsNullOrWhiteSpace(response.Message)
? "کد وارد شده صحیح نیست."
: response.Message;
_errorMessage = _attemptsLeft > 0
? $"{baseMessage} {_attemptsLeft} تلاش باقی‌مانده است."
: $"{baseMessage} تعداد تلاش‌های مجاز شما پایان یافته است. لطفاً رمز جدید دریافت کنید.";
}
}
private async Task HandleVerifyRpcFailureAsync(RpcException rpcEx)
{ {
switch (rpcEx.Status.StatusCode) switch (rpcEx.Status.StatusCode)
{ {
case StatusCode.PermissionDenied: case StatusCode.PermissionDenied:
case StatusCode.InvalidArgument: case StatusCode.InvalidArgument:
RegisterFailedAttempt(!string.IsNullOrWhiteSpace(rpcEx.Status.Detail) ApplyVerificationState(new VerifyOtpTokenResponse
? rpcEx.Status.Detail {
: "کد نادرست است."); Success = false,
Message = string.IsNullOrWhiteSpace(rpcEx.Status.Detail) ? "کد وارد شده صحیح نیست." : rpcEx.Status.Detail,
RemainingAttempts = _attemptsLeft - 1
});
break; break;
case StatusCode.Unauthenticated: case StatusCode.Unauthenticated:
await ResetAuthenticationAsync(); await ResetAuthenticationAsync();
_errorMessage = "نشست کاربری منقضی شده است. لطفاً دوباره وارد شوید."; _errorMessage = string.IsNullOrWhiteSpace(rpcEx.Status.Detail)
? "نشست کاربری منقضی شده است. لطفاً دوباره وارد شوید."
: rpcEx.Status.Detail;
Navigation.NavigateTo(RouteConstants.Auth.Phone, forceLoad: true); Navigation.NavigateTo(RouteConstants.Auth.Phone, forceLoad: true);
break; break;
case StatusCode.DeadlineExceeded: case StatusCode.DeadlineExceeded:
@@ -206,23 +232,9 @@ public partial class Verify : IDisposable
} }
} }
private void RegisterFailedAttempt(string baseMessage)
{
_attemptsLeft = Math.Max(0, _attemptsLeft - 1);
if (_attemptsLeft > 0)
{
_errorMessage = $"{baseMessage} {_attemptsLeft} تلاش باقی مانده است.";
}
else
{
_errorMessage = $"{baseMessage} تلاش‌های مجاز شما به پایان رسیده است. لطفاً رمز جدید دریافت کنید.";
}
}
private async Task ResendOtpAsync() private async Task ResendOtpAsync()
{ {
if (_resendRemaining > 0 || _isBusy || string.IsNullOrWhiteSpace(_phoneNumber)) if (_resendSeconds > 0 || _isBusy || string.IsNullOrWhiteSpace(_phoneNumber))
{ {
return; return;
} }
@@ -241,35 +253,28 @@ public partial class Verify : IDisposable
}; };
var metadata = await BuildAuthMetadataAsync(); var metadata = await BuildAuthMetadataAsync();
CreateNewOtpTokenResponse response; var response = metadata is not null
if (metadata is not null) ? await UserClient.CreateNewOtpTokenAsync(request, metadata, cancellationToken: cancellationToken)
{ : await UserClient.CreateNewOtpTokenAsync(request, cancellationToken: cancellationToken);
response = await UserClient.CreateNewOtpTokenAsync(request, metadata, cancellationToken: cancellationToken);
}
else
{
response = await UserClient.CreateNewOtpTokenAsync(request, cancellationToken: cancellationToken);
}
if (response?.Success != true) if (response?.Success != true)
{ {
_errorMessage = string.IsNullOrWhiteSpace(response?.Message) _errorMessage = !string.IsNullOrWhiteSpace(response?.Message)
? "ارسال مجدد رمز پویا با خطا مواجه شد." ? response!.Message
: response!.Message; : "ارسال مجدد رمز با خطا مواجه شد.";
return; }
else
{
_infoMessage = string.IsNullOrWhiteSpace(response.Message)
? "رمز جدید ارسال شد."
: response.Message;
} }
_infoMessage = string.IsNullOrWhiteSpace(response.Message) ApplyResendState(response);
? "کد جدید ارسال شد."
: response.Message;
_attemptsLeft = MaxVerificationAttempts;
_request.Code = string.Empty;
StartResendCountdown();
} }
catch (RpcException rpcEx) catch (RpcException rpcEx)
{ {
await HandleResendFailureAsync(rpcEx); await HandleResendRpcFailureAsync(rpcEx);
} }
catch (OperationCanceledException) catch (OperationCanceledException)
{ {
@@ -286,50 +291,52 @@ public partial class Verify : IDisposable
} }
} }
private async Task HandleResendFailureAsync(RpcException rpcEx) private async Task HandleResendRpcFailureAsync(RpcException rpcEx)
{ {
switch (rpcEx.Status.StatusCode) switch (rpcEx.Status.StatusCode)
{ {
case StatusCode.Unauthenticated: case StatusCode.Unauthenticated:
await ResetAuthenticationAsync(); await ResetAuthenticationAsync();
_errorMessage = "نشست کاربری منقضی شده است. لطفاً دوباره وارد شوید."; _errorMessage = string.IsNullOrWhiteSpace(rpcEx.Status.Detail)
? "نشست کاربری منقضی شده است. لطفاً دوباره وارد شوید."
: rpcEx.Status.Detail;
Navigation.NavigateTo(RouteConstants.Auth.Phone, forceLoad: true); Navigation.NavigateTo(RouteConstants.Auth.Phone, forceLoad: true);
break; break;
default: default:
_errorMessage = !string.IsNullOrWhiteSpace(rpcEx.Status.Detail) _errorMessage = !string.IsNullOrWhiteSpace(rpcEx.Status.Detail)
? rpcEx.Status.Detail ? rpcEx.Status.Detail
: "ارسال مجدد رمز پویا با خطا مواجه شد."; : "ارسال مجدد رمز با خطا مواجه شد.";
break; break;
} }
} }
private async Task ChangePhoneAsync() private void ApplyResendState(CreateNewOtpTokenResponse? response)
{ {
await LocalStorage.RemoveItemAsync(PhoneStorageKey); if (response?.RemainingAttempts >= 0)
NavigateBackToPhone();
}
private void NavigateBackToPhone()
{
var target = RouteConstants.Auth.Phone;
if (!string.IsNullOrWhiteSpace(_redirect))
{ {
target += "?redirect=" + Uri.EscapeDataString(_redirect); _attemptsLeft = response.RemainingAttempts;
} }
Navigation.NavigateTo(target, forceLoad: false); if (response?.RemainingSeconds > 0)
{
StartResendCountdown(response.RemainingSeconds);
}
else
{
StartResendCountdown(DefaultResendCooldown);
}
} }
private void StartResendCountdown(int seconds = DefaultResendCooldown) private void StartResendCountdown(int seconds)
{ {
_resendRemaining = seconds; _resendSeconds = seconds;
_resendTimer?.Dispose(); _resendTimer?.Dispose();
_resendTimer = new Timer(_ => _resendTimer = new Timer(_ =>
{ {
var remaining = Interlocked.Add(ref _resendRemaining, -1); var remaining = Interlocked.Decrement(ref _resendSeconds);
if (remaining <= 0) if (remaining <= 0)
{ {
Interlocked.Exchange(ref _resendRemaining, 0); Interlocked.Exchange(ref _resendSeconds, 0);
_resendTimer?.Dispose(); _resendTimer?.Dispose();
_resendTimer = null; _resendTimer = null;
} }
@@ -338,6 +345,13 @@ public partial class Verify : IDisposable
}, null, 1000, 1000); }, null, 1000, 1000);
} }
private void ResetResendCountdown()
{
_resendTimer?.Dispose();
_resendTimer = null;
_resendSeconds = 0;
}
private async Task<Metadata?> BuildAuthMetadataAsync() private async Task<Metadata?> BuildAuthMetadataAsync()
{ {
var token = await LocalStorage.GetItemAsync<string>(TokenStorageKey); var token = await LocalStorage.GetItemAsync<string>(TokenStorageKey);
@@ -352,6 +366,24 @@ public partial class Verify : IDisposable
}; };
} }
private async Task PersistTokenAsync(string? token)
{
if (!string.IsNullOrWhiteSpace(token))
{
await LocalStorage.SetItemAsync(TokenStorageKey, token);
}
else
{
await LocalStorage.RemoveItemAsync(TokenStorageKey);
}
}
private async Task ChangePhoneAsync()
{
await LocalStorage.RemoveItemAsync(PhoneStorageKey);
NavigateBackToPhone();
}
private async Task ResetAuthenticationAsync() private async Task ResetAuthenticationAsync()
{ {
await LocalStorage.RemoveItemAsync(TokenStorageKey); await LocalStorage.RemoveItemAsync(TokenStorageKey);
@@ -359,6 +391,17 @@ public partial class Verify : IDisposable
await LocalStorage.RemoveItemAsync(RedirectStorageKey); await LocalStorage.RemoveItemAsync(RedirectStorageKey);
} }
private void NavigateBackToPhone()
{
var target = RouteConstants.Auth.Phone;
if (!string.IsNullOrWhiteSpace(_redirect))
{
target += "?redirect=" + Uri.EscapeDataString(_redirect);
}
Navigation.NavigateTo(target, forceLoad: false);
}
private CancellationToken PrepareOperationToken() private CancellationToken PrepareOperationToken()
{ {
_operationCts?.Cancel(); _operationCts?.Cancel();
@@ -377,9 +420,6 @@ public partial class Verify : IDisposable
{ {
_operationCts?.Cancel(); _operationCts?.Cancel();
_operationCts?.Dispose(); _operationCts?.Dispose();
_operationCts = null;
_resendTimer?.Dispose(); _resendTimer?.Dispose();
_resendTimer = null;
} }
} }