Files
FrontOffice/src/FrontOffice.Main/Pages/Auth/Verify.razor.cs

426 lines
13 KiB
C#
Raw Normal View History

2025-09-28 08:38:13 +03:30
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Blazored.LocalStorage;
2025-09-28 03:24:54 +03:30
using FrontOffice.BFF.User.Protobuf.Protos.User;
2025-09-28 03:49:17 +03:30
using FrontOffice.BFF.User.Protobuf.Validator;
2025-09-28 03:24:54 +03:30
using FrontOffice.Main.Utilities;
using Grpc.Core;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.WebUtilities;
using MudBlazor;
namespace FrontOffice.Main.Pages.Auth;
public partial class Verify : IDisposable
{
private const int DefaultResendCooldown = 120;
public const int MaxVerificationAttempts = 5;
private const string OtpPurpose = "Login";
private const string PhoneStorageKey = "auth:phone-number";
private const string RedirectStorageKey = "auth:redirect";
private const string TokenStorageKey = "auth:token";
2025-09-28 08:38:13 +03:30
private readonly VerifyOtpTokenRequestValidator _requestValidator = new();
private readonly VerifyOtpTokenRequest _request = new();
2025-09-28 05:36:45 +03:30
2025-09-28 08:38:13 +03:30
private MudForm? _form;
2025-09-28 03:24:54 +03:30
private bool _isBusy;
private string? _phoneNumber;
private string? _redirect;
private string? _errorMessage;
private string? _infoMessage;
private Timer? _resendTimer;
2025-09-28 08:38:13 +03:30
private int _resendSeconds;
2025-09-28 03:24:54 +03:30
private int _attemptsLeft = MaxVerificationAttempts;
private CancellationTokenSource? _operationCts;
[Inject] private ILocalStorageService LocalStorage { get; set; } = default!;
[Inject] private UserContract.UserContractClient UserClient { get; set; } = default!;
private bool IsVerificationLocked => _attemptsLeft <= 0;
protected override async Task OnInitializedAsync()
{
2025-09-28 05:36:45 +03:30
_request.Purpose = OtpPurpose;
2025-09-28 03:24:54 +03:30
var uri = Navigation.ToAbsoluteUri(Navigation.Uri);
var query = QueryHelpers.ParseQuery(uri.Query);
if (query.TryGetValue("redirect", out var redirectValues))
{
_redirect = redirectValues.LastOrDefault();
}
if (query.TryGetValue("phone", out var phoneValues))
{
_phoneNumber = phoneValues.LastOrDefault();
}
if (string.IsNullOrWhiteSpace(_phoneNumber))
{
_phoneNumber = await LocalStorage.GetItemAsync<string>(PhoneStorageKey);
}
if (string.IsNullOrWhiteSpace(_phoneNumber))
{
2025-09-28 03:49:17 +03:30
await ResetAuthenticationAsync();
Navigation.NavigateTo(RouteConstants.Auth.Phone, forceLoad: true);
2025-09-28 03:24:54 +03:30
return;
}
await LocalStorage.SetItemAsync(PhoneStorageKey, _phoneNumber);
if (string.IsNullOrWhiteSpace(_redirect))
{
var storedRedirect = await LocalStorage.GetItemAsync<string>(RedirectStorageKey);
if (!string.IsNullOrWhiteSpace(storedRedirect))
{
_redirect = storedRedirect;
}
}
2025-09-28 08:38:13 +03:30
ResetResendCountdown();
2025-09-28 03:24:54 +03:30
}
private async Task VerifyOtpAsync()
{
_errorMessage = null;
_infoMessage = null;
if (_form is null)
2025-09-28 08:38:13 +03:30
{
return;
}
2025-09-28 03:24:54 +03:30
await _form.Validate();
if (!_form.IsValid)
2025-09-28 08:38:13 +03:30
{
2025-09-28 03:24:54 +03:30
return;
2025-09-28 08:38:13 +03:30
}
2025-09-28 03:24:54 +03:30
if (IsVerificationLocked)
{
2025-09-28 08:38:13 +03:30
_errorMessage = "تعداد تلاش‌های مجاز شما به پایان رسیده است. لطفاً رمز جدید دریافت کنید.";
2025-09-28 03:24:54 +03:30
return;
}
if (string.IsNullOrWhiteSpace(_phoneNumber))
{
2025-09-28 03:49:17 +03:30
await ResetAuthenticationAsync();
Navigation.NavigateTo(RouteConstants.Auth.Phone, forceLoad: true);
2025-09-28 03:24:54 +03:30
return;
}
_isBusy = true;
var cancellationToken = PrepareOperationToken();
try
{
2025-09-28 05:36:45 +03:30
_request.Mobile = _phoneNumber;
var validationResult = _requestValidator.Validate(_request);
2025-09-28 03:49:17 +03:30
if (!validationResult.IsValid)
{
_errorMessage = string.Join(" ", validationResult.Errors.Select(e => e.ErrorMessage).Distinct());
return;
}
var metadata = await BuildAuthMetadataAsync();
2025-09-28 08:38:13 +03:30
var response = metadata is not null
? await UserClient.VerifyOtpTokenAsync(_request, metadata, cancellationToken: cancellationToken)
: await UserClient.VerifyOtpTokenAsync(_request, cancellationToken: cancellationToken);
2025-09-28 03:49:17 +03:30
2025-09-28 03:24:54 +03:30
if (response is null)
{
2025-09-28 03:49:17 +03:30
_errorMessage = "تأیید رمز پویا انجام نشد. لطفاً دوباره تلاش کنید.";
2025-09-28 03:24:54 +03:30
return;
}
2025-09-28 08:38:13 +03:30
ApplyVerificationState(response);
2025-09-28 03:24:54 +03:30
if (response.Success)
{
2025-09-28 08:38:13 +03:30
await PersistTokenAsync(response.Token);
2025-09-28 03:24:54 +03:30
await LocalStorage.RemoveItemAsync(PhoneStorageKey);
await LocalStorage.RemoveItemAsync(RedirectStorageKey);
var target = !string.IsNullOrWhiteSpace(_redirect) ? _redirect : RouteConstants.Main.MainPage;
2025-09-28 08:38:13 +03:30
Navigation.NavigateTo(target ?? RouteConstants.Main.MainPage, forceLoad: true);
2025-09-28 03:24:54 +03:30
}
}
catch (RpcException rpcEx)
{
2025-09-28 08:38:13 +03:30
await HandleVerifyRpcFailureAsync(rpcEx);
2025-09-28 03:24:54 +03:30
}
catch (OperationCanceledException)
{
}
catch (Exception ex)
{
_errorMessage = ex.Message;
}
finally
{
_isBusy = false;
ClearOperationToken();
await InvokeAsync(StateHasChanged);
}
}
2025-09-28 08:38:13 +03:30
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)
2025-09-28 03:24:54 +03:30
{
switch (rpcEx.Status.StatusCode)
{
case StatusCode.PermissionDenied:
case StatusCode.InvalidArgument:
2025-09-28 08:38:13 +03:30
ApplyVerificationState(new VerifyOtpTokenResponse
{
Success = false,
Message = string.IsNullOrWhiteSpace(rpcEx.Status.Detail) ? "کد وارد شده صحیح نیست." : rpcEx.Status.Detail,
RemainingAttempts = _attemptsLeft - 1
});
2025-09-28 03:49:17 +03:30
break;
case StatusCode.Unauthenticated:
await ResetAuthenticationAsync();
2025-09-28 08:38:13 +03:30
_errorMessage = string.IsNullOrWhiteSpace(rpcEx.Status.Detail)
? "نشست کاربری منقضی شده است. لطفاً دوباره وارد شوید."
: rpcEx.Status.Detail;
2025-09-28 03:49:17 +03:30
Navigation.NavigateTo(RouteConstants.Auth.Phone, forceLoad: true);
2025-09-28 03:24:54 +03:30
break;
case StatusCode.DeadlineExceeded:
case StatusCode.NotFound:
2025-09-28 03:49:17 +03:30
_errorMessage = "کد منقضی شده است. لطفاً رمز جدید دریافت کنید.";
2025-09-28 03:24:54 +03:30
break;
default:
_errorMessage = !string.IsNullOrWhiteSpace(rpcEx.Status.Detail)
? rpcEx.Status.Detail
2025-09-28 03:49:17 +03:30
: "تأیید رمز پویا انجام نشد. لطفاً دوباره تلاش کنید.";
2025-09-28 03:24:54 +03:30
break;
}
}
private async Task ResendOtpAsync()
{
2025-09-28 08:38:13 +03:30
if (_resendSeconds > 0 || _isBusy || string.IsNullOrWhiteSpace(_phoneNumber))
2025-09-28 03:24:54 +03:30
{
return;
}
_errorMessage = null;
_infoMessage = null;
_isBusy = true;
var cancellationToken = PrepareOperationToken();
try
{
var request = new CreateNewOtpTokenRequest
{
Mobile = _phoneNumber,
Purpose = OtpPurpose
};
2025-09-28 03:49:17 +03:30
var metadata = await BuildAuthMetadataAsync();
2025-09-28 08:38:13 +03:30
var response = metadata is not null
? await UserClient.CreateNewOtpTokenAsync(request, metadata, cancellationToken: cancellationToken)
: await UserClient.CreateNewOtpTokenAsync(request, cancellationToken: cancellationToken);
if (response?.Success != true)
2025-09-28 03:49:17 +03:30
{
2025-09-28 08:38:13 +03:30
_errorMessage = !string.IsNullOrWhiteSpace(response?.Message)
? response!.Message
: "ارسال مجدد رمز با خطا مواجه شد.";
2025-09-28 03:49:17 +03:30
}
else
{
2025-09-28 08:38:13 +03:30
_infoMessage = string.IsNullOrWhiteSpace(response.Message)
? "رمز جدید ارسال شد."
: response.Message;
2025-09-28 03:49:17 +03:30
}
2025-09-28 08:38:13 +03:30
ApplyResendState(response);
2025-09-28 03:24:54 +03:30
}
catch (RpcException rpcEx)
{
2025-09-28 08:38:13 +03:30
await HandleResendRpcFailureAsync(rpcEx);
2025-09-28 03:24:54 +03:30
}
catch (OperationCanceledException)
{
}
catch (Exception ex)
{
_errorMessage = ex.Message;
}
finally
{
_isBusy = false;
ClearOperationToken();
await InvokeAsync(StateHasChanged);
}
}
2025-09-28 08:38:13 +03:30
private async Task HandleResendRpcFailureAsync(RpcException rpcEx)
2025-09-28 03:49:17 +03:30
{
switch (rpcEx.Status.StatusCode)
{
case StatusCode.Unauthenticated:
await ResetAuthenticationAsync();
2025-09-28 08:38:13 +03:30
_errorMessage = string.IsNullOrWhiteSpace(rpcEx.Status.Detail)
? "نشست کاربری منقضی شده است. لطفاً دوباره وارد شوید."
: rpcEx.Status.Detail;
2025-09-28 03:49:17 +03:30
Navigation.NavigateTo(RouteConstants.Auth.Phone, forceLoad: true);
break;
default:
_errorMessage = !string.IsNullOrWhiteSpace(rpcEx.Status.Detail)
? rpcEx.Status.Detail
2025-09-28 08:38:13 +03:30
: "ارسال مجدد رمز با خطا مواجه شد.";
2025-09-28 03:49:17 +03:30
break;
}
}
2025-09-28 08:38:13 +03:30
private void ApplyResendState(CreateNewOtpTokenResponse? response)
2025-09-28 03:24:54 +03:30
{
2025-09-28 08:38:13 +03:30
if (response?.RemainingAttempts >= 0)
2025-09-28 03:24:54 +03:30
{
2025-09-28 08:38:13 +03:30
_attemptsLeft = response.RemainingAttempts;
2025-09-28 03:24:54 +03:30
}
2025-09-28 08:38:13 +03:30
if (response?.RemainingSeconds > 0)
{
StartResendCountdown(response.RemainingSeconds);
}
else
{
StartResendCountdown(DefaultResendCooldown);
}
2025-09-28 03:24:54 +03:30
}
2025-09-28 08:38:13 +03:30
private void StartResendCountdown(int seconds)
2025-09-28 03:24:54 +03:30
{
2025-09-28 08:38:13 +03:30
_resendSeconds = seconds;
2025-09-28 03:24:54 +03:30
_resendTimer?.Dispose();
_resendTimer = new Timer(_ =>
{
2025-09-28 08:38:13 +03:30
var remaining = Interlocked.Decrement(ref _resendSeconds);
2025-09-28 03:24:54 +03:30
if (remaining <= 0)
{
2025-09-28 08:38:13 +03:30
Interlocked.Exchange(ref _resendSeconds, 0);
2025-09-28 03:24:54 +03:30
_resendTimer?.Dispose();
_resendTimer = null;
}
_ = InvokeAsync(StateHasChanged);
}, null, 1000, 1000);
}
2025-09-28 08:38:13 +03:30
private void ResetResendCountdown()
{
_resendTimer?.Dispose();
_resendTimer = null;
_resendSeconds = 0;
}
2025-09-28 03:49:17 +03:30
private async Task<Metadata?> BuildAuthMetadataAsync()
{
var token = await LocalStorage.GetItemAsync<string>(TokenStorageKey);
if (string.IsNullOrWhiteSpace(token))
{
return null;
}
return new Metadata
{
{ "Authorization", $"Bearer {token}" }
};
}
2025-09-28 08:38:13 +03:30
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();
}
2025-09-28 03:49:17 +03:30
private async Task ResetAuthenticationAsync()
{
await LocalStorage.RemoveItemAsync(TokenStorageKey);
await LocalStorage.RemoveItemAsync(PhoneStorageKey);
await LocalStorage.RemoveItemAsync(RedirectStorageKey);
}
2025-09-28 08:38:13 +03:30
private void NavigateBackToPhone()
{
var target = RouteConstants.Auth.Phone;
if (!string.IsNullOrWhiteSpace(_redirect))
{
target += "?redirect=" + Uri.EscapeDataString(_redirect);
}
Navigation.NavigateTo(target, forceLoad: false);
}
2025-09-28 03:24:54 +03:30
private CancellationToken PrepareOperationToken()
{
_operationCts?.Cancel();
_operationCts?.Dispose();
_operationCts = new CancellationTokenSource();
return _operationCts.Token;
}
private void ClearOperationToken()
{
_operationCts?.Dispose();
_operationCts = null;
}
public void Dispose()
{
_operationCts?.Cancel();
_operationCts?.Dispose();
_resendTimer?.Dispose();
}
}