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,4 +1,4 @@
@page "/auth/phone" @attribute [Route(RouteConstants.Auth.Phone)]
<PageTitle>ورود | تأیید شماره موبایل</PageTitle> <PageTitle>ورود | تأیید شماره موبایل</PageTitle>
@@ -52,3 +52,4 @@

View File

@@ -1,10 +1,8 @@
using System; using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Blazored.LocalStorage; 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.Main.Utilities;
using Grpc.Core; using Grpc.Core;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.WebUtilities; using Microsoft.AspNetCore.WebUtilities;
@@ -16,8 +14,11 @@ public partial class Phone : IDisposable
{ {
private const string PhoneStorageKey = "auth:phone-number"; private const string PhoneStorageKey = "auth:phone-number";
private const string RedirectStorageKey = "auth:redirect"; private const string RedirectStorageKey = "auth:redirect";
private const string TokenStorageKey = "auth:token";
private const string OtpPurpose = "Login"; private const string OtpPurpose = "Login";
private static readonly CreateNewOtpTokenRequestValidator RequestValidator = new();
private readonly PhoneInputModel _model = new(); private readonly PhoneInputModel _model = new();
private MudForm? _form; private MudForm? _form;
private bool _isBusy; private bool _isBusy;
@@ -80,7 +81,24 @@ public partial class Phone : IDisposable
Purpose = OtpPurpose 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) if (response?.Success != true)
{ {
_errorMessage = string.IsNullOrWhiteSpace(response?.Message) _errorMessage = string.IsNullOrWhiteSpace(response?.Message)
@@ -99,7 +117,7 @@ public partial class Phone : IDisposable
await LocalStorage.RemoveItemAsync(RedirectStorageKey); 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)) if (!string.IsNullOrEmpty(_redirect))
{ {
target += "&redirect=" + Uri.EscapeDataString(_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() public void Dispose()
{ {
_sendCts?.Cancel(); _sendCts?.Cancel();
@@ -138,12 +170,11 @@ public partial class Phone : IDisposable
private sealed class PhoneInputModel private sealed class PhoneInputModel
{ {
[Required(ErrorMessage = "وارد کردن شماره موبایل الزامی است.")] [Required(ErrorMessage = "???? ???? ????? ?????? ?????? ???.")]
[RegularExpression(@"^09\\d{9}$", ErrorMessage = "شماره موبایل معتبر نیست.")] [RegularExpression(@"^09\\d{9}$", ErrorMessage = "????? ?????? ????? ????.")]
public string PhoneNumber { get; set; } = string.Empty; public string PhoneNumber { get; set; } = string.Empty;
[Range(typeof(bool), "true", "true", ErrorMessage = "پذیرش شرایط و قوانین ضروری است.")] [Range(typeof(bool), "true", "true", ErrorMessage = "????? ????? ? ?????? ????? ???.")]
public bool AcceptTerms { get; set; } public bool AcceptTerms { get; set; }
} }
} }

View File

@@ -1,4 +1,4 @@
@page "/auth/verify" @attribute [Route(RouteConstants.Auth.Verify)]
<PageTitle>تأیید رمز پویا</PageTitle> <PageTitle>تأیید رمز پویا</PageTitle>
@@ -82,3 +82,4 @@

View File

@@ -1,10 +1,7 @@
using System; using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Blazored.LocalStorage; 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.Main.Utilities; using FrontOffice.Main.Utilities;
using Grpc.Core; using Grpc.Core;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
@@ -22,6 +19,8 @@ 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 static readonly VerifyOtpTokenRequestValidator VerifyRequestValidator = new();
private readonly OtpInputModel _model = new(); private readonly OtpInputModel _model = new();
private MudForm? _form; private MudForm? _form;
private bool _isBusy; private bool _isBusy;
@@ -60,7 +59,8 @@ public partial class Verify : IDisposable
if (string.IsNullOrWhiteSpace(_phoneNumber)) if (string.IsNullOrWhiteSpace(_phoneNumber))
{ {
NavigateBackToPhone(); await ResetAuthenticationAsync();
Navigation.NavigateTo(RouteConstants.Auth.Phone, forceLoad: true);
return; return;
} }
@@ -96,13 +96,14 @@ public partial class Verify : IDisposable
if (IsVerificationLocked) if (IsVerificationLocked)
{ {
_errorMessage = "???????? ???? ?? ????? ????? ???. ????? ??? ???? ?????? ????."; _errorMessage = "تعداد تلاش‌های مجاز به پایان رسیده است. لطفاً رمز جدید دریافت کنید.";
return; return;
} }
if (string.IsNullOrWhiteSpace(_phoneNumber)) if (string.IsNullOrWhiteSpace(_phoneNumber))
{ {
NavigateBackToPhone(); await ResetAuthenticationAsync();
Navigation.NavigateTo(RouteConstants.Auth.Phone, forceLoad: true);
return; return;
} }
@@ -118,10 +119,27 @@ public partial class Verify : IDisposable
Code = _model.Code Code = _model.Code
}; };
var response = await UserClient.VerifyOtpTokenAsync(request, cancellationToken: cancellationToken); var validationResult = VerifyRequestValidator.Validate(request);
if (!validationResult.IsValid)
{
_errorMessage = string.Join(" ", validationResult.Errors.Select(e => e.ErrorMessage).Distinct());
return;
}
var metadata = await BuildAuthMetadataAsync();
VerifyOtpTokenResponse response;
if (metadata is not null)
{
response = await UserClient.VerifyOtpTokenAsync(request, metadata, cancellationToken: cancellationToken);
}
else
{
response = await UserClient.VerifyOtpTokenAsync(request, cancellationToken: cancellationToken);
}
if (response is null) if (response is null)
{ {
_errorMessage = "????? ??? ???? ????? ???. ????? ?????? ???? ????."; _errorMessage = "تأیید رمز پویا انجام نشد. لطفاً دوباره تلاش کنید.";
return; return;
} }
@@ -131,6 +149,10 @@ public partial class Verify : IDisposable
{ {
await LocalStorage.SetItemAsync(TokenStorageKey, 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);
@@ -146,11 +168,11 @@ public partial class Verify : IDisposable
return; return;
} }
RegisterFailedAttempt(string.IsNullOrWhiteSpace(response.Message) ? "?? ?????? ???." : response.Message); RegisterFailedAttempt(string.IsNullOrWhiteSpace(response.Message) ? "کد نادرست است." : response.Message);
} }
catch (RpcException rpcEx) catch (RpcException rpcEx)
{ {
HandleVerificationFailure(rpcEx); await HandleVerificationFailureAsync(rpcEx);
} }
catch (OperationCanceledException) catch (OperationCanceledException)
{ {
@@ -167,25 +189,29 @@ public partial class Verify : IDisposable
} }
} }
private void HandleVerificationFailure(RpcException rpcEx) private async Task HandleVerificationFailureAsync(RpcException rpcEx)
{ {
switch (rpcEx.Status.StatusCode) switch (rpcEx.Status.StatusCode)
{ {
case StatusCode.PermissionDenied: case StatusCode.PermissionDenied:
case StatusCode.InvalidArgument: case StatusCode.InvalidArgument:
case StatusCode.Unauthenticated:
RegisterFailedAttempt(!string.IsNullOrWhiteSpace(rpcEx.Status.Detail) RegisterFailedAttempt(!string.IsNullOrWhiteSpace(rpcEx.Status.Detail)
? rpcEx.Status.Detail ? rpcEx.Status.Detail
: "?? ?????? ???."); : "کد نادرست است.");
break;
case StatusCode.Unauthenticated:
await ResetAuthenticationAsync();
_errorMessage = "نشست کاربری منقضی شده است. لطفاً دوباره وارد شوید.";
Navigation.NavigateTo(RouteConstants.Auth.Phone, forceLoad: true);
break; break;
case StatusCode.DeadlineExceeded: case StatusCode.DeadlineExceeded:
case StatusCode.NotFound: case StatusCode.NotFound:
_errorMessage = "?? ????? ??? ???. ????? ??? ???? ?????? ????."; _errorMessage = "کد منقضی شده است. لطفاً رمز جدید دریافت کنید.";
break; break;
default: default:
_errorMessage = !string.IsNullOrWhiteSpace(rpcEx.Status.Detail) _errorMessage = !string.IsNullOrWhiteSpace(rpcEx.Status.Detail)
? rpcEx.Status.Detail ? rpcEx.Status.Detail
: "????? ??? ???? ????? ???. ????? ?????? ???? ????."; : "تأیید رمز پویا انجام نشد. لطفاً دوباره تلاش کنید.";
break; break;
} }
} }
@@ -196,11 +222,11 @@ public partial class Verify : IDisposable
if (_attemptsLeft > 0) if (_attemptsLeft > 0)
{ {
_errorMessage = $"{baseMessage} {_attemptsLeft} ???? ???? ????? ???."; _errorMessage = $"{baseMessage} {_attemptsLeft} تلاش باقی مانده است.";
} }
else else
{ {
_errorMessage = $"{baseMessage} ???????? ???? ??? ?? ????? ????? ???. ????? ??? ???? ?????? ????."; _errorMessage = $"{baseMessage} تلاش‌های مجاز شما به پایان رسیده است. لطفاً رمز جدید دریافت کنید.";
} }
} }
@@ -224,17 +250,27 @@ public partial class Verify : IDisposable
Purpose = OtpPurpose Purpose = OtpPurpose
}; };
var response = await UserClient.CreateNewOtpTokenAsync(request, cancellationToken: cancellationToken); var metadata = await BuildAuthMetadataAsync();
CreateNewOtpTokenResponse response;
if (metadata is not null)
{
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; return;
} }
_infoMessage = string.IsNullOrWhiteSpace(response.Message) _infoMessage = string.IsNullOrWhiteSpace(response.Message)
? "?? ???? ????? ??." ? "کد جدید ارسال شد."
: response.Message; : response.Message;
_attemptsLeft = MaxVerificationAttempts; _attemptsLeft = MaxVerificationAttempts;
@@ -243,9 +279,7 @@ public partial class Verify : IDisposable
} }
catch (RpcException rpcEx) catch (RpcException rpcEx)
{ {
_errorMessage = !string.IsNullOrWhiteSpace(rpcEx.Status.Detail) await HandleResendFailureAsync(rpcEx);
? rpcEx.Status.Detail
: "????? ???? ??? ???? ?? ??? ????? ??.";
} }
catch (OperationCanceledException) catch (OperationCanceledException)
{ {
@@ -262,6 +296,23 @@ public partial class Verify : IDisposable
} }
} }
private async Task HandleResendFailureAsync(RpcException rpcEx)
{
switch (rpcEx.Status.StatusCode)
{
case StatusCode.Unauthenticated:
await ResetAuthenticationAsync();
_errorMessage = "نشست کاربری منقضی شده است. لطفاً دوباره وارد شوید.";
Navigation.NavigateTo(RouteConstants.Auth.Phone, forceLoad: true);
break;
default:
_errorMessage = !string.IsNullOrWhiteSpace(rpcEx.Status.Detail)
? rpcEx.Status.Detail
: "ارسال مجدد رمز پویا با خطا مواجه شد.";
break;
}
}
private async Task ChangePhoneAsync() private async Task ChangePhoneAsync()
{ {
await LocalStorage.RemoveItemAsync(PhoneStorageKey); await LocalStorage.RemoveItemAsync(PhoneStorageKey);
@@ -270,7 +321,7 @@ public partial class Verify : IDisposable
private void NavigateBackToPhone() private void NavigateBackToPhone()
{ {
var target = "/auth/phone"; var target = RouteConstants.Auth.Phone;
if (!string.IsNullOrWhiteSpace(_redirect)) if (!string.IsNullOrWhiteSpace(_redirect))
{ {
target += "?redirect=" + Uri.EscapeDataString(_redirect); target += "?redirect=" + Uri.EscapeDataString(_redirect);
@@ -297,6 +348,27 @@ public partial class Verify : IDisposable
}, null, 1000, 1000); }, null, 1000, 1000);
} }
private async Task<Metadata?> BuildAuthMetadataAsync()
{
var token = await LocalStorage.GetItemAsync<string>(TokenStorageKey);
if (string.IsNullOrWhiteSpace(token))
{
return null;
}
return new Metadata
{
{ "Authorization", $"Bearer {token}" }
};
}
private async Task ResetAuthenticationAsync()
{
await LocalStorage.RemoveItemAsync(TokenStorageKey);
await LocalStorage.RemoveItemAsync(PhoneStorageKey);
await LocalStorage.RemoveItemAsync(RedirectStorageKey);
}
private CancellationToken PrepareOperationToken() private CancellationToken PrepareOperationToken()
{ {
_operationCts?.Cancel(); _operationCts?.Cancel();
@@ -328,6 +400,3 @@ public partial class Verify : IDisposable
public string Code { get; set; } = string.Empty; public string Code { get; set; } = string.Empty;
} }
} }

View File

@@ -1,4 +1,4 @@
namespace FrontOffice.Main.Utilities; namespace FrontOffice.Main.Utilities;
public static class RouteConstants public static class RouteConstants
{ {
@@ -6,4 +6,10 @@ public static class RouteConstants
{ {
public const string MainPage = "/"; public const string MainPage = "/";
} }
public static class Auth
{
public const string Phone = "/auth/phone";
public const string Verify = "/auth/verify";
}
} }