This commit is contained in:
MeysamMoghaddam
2025-09-28 03:24:54 +03:30
parent 1b8a584435
commit d4b5a1352c
4 changed files with 620 additions and 0 deletions

View File

@@ -0,0 +1,149 @@
using System;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Blazored.LocalStorage;
using FrontOffice.BFF.User.Protobuf.Protos.User;
using Grpc.Core;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.WebUtilities;
using MudBlazor;
namespace FrontOffice.Main.Pages.Auth;
public partial class Phone : IDisposable
{
private const string PhoneStorageKey = "auth:phone-number";
private const string RedirectStorageKey = "auth:redirect";
private const string OtpPurpose = "Login";
private readonly PhoneInputModel _model = new();
private MudForm? _form;
private bool _isBusy;
private string? _errorMessage;
private string? _redirect;
private CancellationTokenSource? _sendCts;
[Inject] private ILocalStorageService LocalStorage { get; set; } = default!;
[Inject] private UserContract.UserContractClient UserClient { get; set; } = default!;
protected override async Task OnInitializedAsync()
{
var uri = Navigation.ToAbsoluteUri(Navigation.Uri);
var query = QueryHelpers.ParseQuery(uri.Query);
if (query.TryGetValue("redirect", out var redirectValues))
{
_redirect = redirectValues.LastOrDefault();
}
var storedPhone = await LocalStorage.GetItemAsync<string>(PhoneStorageKey);
if (!string.IsNullOrWhiteSpace(storedPhone))
{
_model.PhoneNumber = storedPhone;
}
if (string.IsNullOrWhiteSpace(_redirect))
{
var storedRedirect = await LocalStorage.GetItemAsync<string>(RedirectStorageKey);
if (!string.IsNullOrWhiteSpace(storedRedirect))
{
_redirect = storedRedirect;
}
}
}
private async Task SendOtpAsync()
{
_errorMessage = null;
if (_form is null)
{
return;
}
await _form.Validate();
if (!_form.IsValid)
{
return;
}
_isBusy = true;
_sendCts?.Cancel();
_sendCts?.Dispose();
_sendCts = new CancellationTokenSource();
try
{
var request = new CreateNewOtpTokenRequest
{
Mobile = _model.PhoneNumber,
Purpose = OtpPurpose
};
var response = await UserClient.CreateNewOtpTokenAsync(request, cancellationToken: _sendCts.Token);
if (response?.Success != true)
{
_errorMessage = string.IsNullOrWhiteSpace(response?.Message)
? "ارسال رمز پویا با خطا مواجه شد. لطفاً دوباره تلاش کنید."
: response!.Message;
return;
}
await LocalStorage.SetItemAsync(PhoneStorageKey, _model.PhoneNumber);
if (!string.IsNullOrWhiteSpace(_redirect))
{
await LocalStorage.SetItemAsync(RedirectStorageKey, _redirect);
}
else
{
await LocalStorage.RemoveItemAsync(RedirectStorageKey);
}
var target = "/auth/verify?phone=" + Uri.EscapeDataString(_model.PhoneNumber);
if (!string.IsNullOrEmpty(_redirect))
{
target += "&redirect=" + Uri.EscapeDataString(_redirect);
}
Navigation.NavigateTo(target, forceLoad: false);
}
catch (RpcException rpcEx)
{
_errorMessage = !string.IsNullOrWhiteSpace(rpcEx.Status.Detail)
? rpcEx.Status.Detail
: "ارسال رمز پویا با خطا مواجه شد. لطفاً دوباره تلاش کنید.";
}
catch (OperationCanceledException)
{
}
catch (Exception ex)
{
_errorMessage = ex.Message;
}
finally
{
_isBusy = false;
_sendCts?.Dispose();
_sendCts = null;
await InvokeAsync(StateHasChanged);
}
}
public void Dispose()
{
_sendCts?.Cancel();
_sendCts?.Dispose();
_sendCts = null;
}
private sealed class PhoneInputModel
{
[Required(ErrorMessage = "وارد کردن شماره موبایل الزامی است.")]
[RegularExpression(@"^09\\d{9}$", ErrorMessage = "شماره موبایل معتبر نیست.")]
public string PhoneNumber { get; set; } = string.Empty;
[Range(typeof(bool), "true", "true", ErrorMessage = "پذیرش شرایط و قوانین ضروری است.")]
public bool AcceptTerms { get; set; }
}
}