This commit is contained in:
MeysamMoghaddam
2025-09-28 03:49:17 +03:30
parent d4b5a1352c
commit f9567c4265
5 changed files with 152 additions and 44 deletions

View File

@@ -1,10 +1,8 @@
using System;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using Blazored.LocalStorage;
using FrontOffice.BFF.User.Protobuf.Protos.User;
using FrontOffice.BFF.User.Protobuf.Validator;
using FrontOffice.Main.Utilities;
using Grpc.Core;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.WebUtilities;
@@ -16,8 +14,11 @@ public partial class Phone : IDisposable
{
private const string PhoneStorageKey = "auth:phone-number";
private const string RedirectStorageKey = "auth:redirect";
private const string TokenStorageKey = "auth:token";
private const string OtpPurpose = "Login";
private static readonly CreateNewOtpTokenRequestValidator RequestValidator = new();
private readonly PhoneInputModel _model = new();
private MudForm? _form;
private bool _isBusy;
@@ -80,7 +81,24 @@ public partial class Phone : IDisposable
Purpose = OtpPurpose
};
var response = await UserClient.CreateNewOtpTokenAsync(request, cancellationToken: _sendCts.Token);
var validationResult = RequestValidator.Validate(request);
if (!validationResult.IsValid)
{
_errorMessage = string.Join(" ", validationResult.Errors.Select(e => e.ErrorMessage).Distinct());
return;
}
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);
}
if (response?.Success != true)
{
_errorMessage = string.IsNullOrWhiteSpace(response?.Message)
@@ -99,7 +117,7 @@ public partial class Phone : IDisposable
await LocalStorage.RemoveItemAsync(RedirectStorageKey);
}
var target = "/auth/verify?phone=" + Uri.EscapeDataString(_model.PhoneNumber);
var target = $"{RouteConstants.Auth.Verify}?phone={Uri.EscapeDataString(_model.PhoneNumber)}";
if (!string.IsNullOrEmpty(_redirect))
{
target += "&redirect=" + Uri.EscapeDataString(_redirect);
@@ -129,6 +147,20 @@ public partial class Phone : IDisposable
}
}
private async Task<Metadata?> BuildAuthMetadataAsync()
{
var token = await LocalStorage.GetItemAsync<string>(TokenStorageKey);
if (string.IsNullOrWhiteSpace(token))
{
return null;
}
return new Metadata
{
{ "Authorization", $"Bearer {token}" }
};
}
public void Dispose()
{
_sendCts?.Cancel();
@@ -138,12 +170,11 @@ public partial class Phone : IDisposable
private sealed class PhoneInputModel
{
[Required(ErrorMessage = "وارد کردن شماره موبایل الزامی است.")]
[RegularExpression(@"^09\\d{9}$", ErrorMessage = "شماره موبایل معتبر نیست.")]
[Required(ErrorMessage = "???? ???? ????? ?????? ?????? ???.")]
[RegularExpression(@"^09\\d{9}$", ErrorMessage = "????? ?????? ????? ????.")]
public string PhoneNumber { get; set; } = string.Empty;
[Range(typeof(bool), "true", "true", ErrorMessage = "پذیرش شرایط و قوانین ضروری است.")]
[Range(typeof(bool), "true", "true", ErrorMessage = "????? ????? ? ?????? ????? ???.")]
public bool AcceptTerms { get; set; }
}
}