feat(client): migrate Login.razor to IAuthApiClient
- Replace IAuthService injection with IAuthApiClient - Keep ICryptoService for credential encryption - Add AuthStateProvider injection to notify authentication state - Use result.Switch() pattern for ApiResult<LoginResultModel> handling - Properly handle ValidationError with FieldErrors dictionary
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
@page "/login"
|
||||
@using JdeScoping.Core.Models.Auth
|
||||
@inject IAuthService AuthService
|
||||
@using JdeScoping.Core.ApiContracts
|
||||
@inject IAuthApiClient AuthApi
|
||||
@inject ICryptoService CryptoService
|
||||
@inject AuthStateProvider AuthStateProvider
|
||||
@inject NavigationManager NavigationManager
|
||||
|
||||
<PageTitle>Login - JDE Scoping Tool</PageTitle>
|
||||
@@ -60,17 +63,47 @@
|
||||
|
||||
try
|
||||
{
|
||||
var result = await AuthService.LoginAsync(_loginModel);
|
||||
// Encrypt credentials
|
||||
var encryptedData = await CryptoService.EncryptLoginAsync(_loginModel);
|
||||
var request = new EncryptedLoginRequest(encryptedData);
|
||||
|
||||
if (result.Success)
|
||||
{
|
||||
var returnUrl = string.IsNullOrEmpty(ReturnUrl) ? "/" : ReturnUrl;
|
||||
NavigationManager.NavigateTo(returnUrl);
|
||||
}
|
||||
else
|
||||
{
|
||||
_errorMessage = result.ErrorMessage ?? "Login failed. Please check your credentials.";
|
||||
}
|
||||
var result = await AuthApi.LoginAsync(request);
|
||||
result.Switch(
|
||||
loginResult =>
|
||||
{
|
||||
if (loginResult.Success && loginResult.User is not null)
|
||||
{
|
||||
// Notify auth state provider of successful login
|
||||
var userViewModel = new JdeScoping.Client.Models.UserInfoViewModel
|
||||
{
|
||||
Username = loginResult.User.Username,
|
||||
FirstName = loginResult.User.FirstName,
|
||||
LastName = loginResult.User.LastName,
|
||||
DisplayName = loginResult.User.DisplayName,
|
||||
EmailAddress = loginResult.User.EmailAddress,
|
||||
Title = loginResult.User.Title
|
||||
};
|
||||
_ = AuthStateProvider.MarkUserAsAuthenticated(userViewModel);
|
||||
|
||||
var returnUrl = string.IsNullOrEmpty(ReturnUrl) ? "/" : ReturnUrl;
|
||||
NavigationManager.NavigateTo(returnUrl);
|
||||
}
|
||||
else
|
||||
{
|
||||
_errorMessage = loginResult.ErrorMessage ?? "Login failed. Please check your credentials.";
|
||||
}
|
||||
},
|
||||
notFound => { _errorMessage = "Authentication service not found."; },
|
||||
validation =>
|
||||
{
|
||||
// ValidationError has FieldErrors dictionary, not Message property
|
||||
var errors = validation.FieldErrors.SelectMany(e => e.Value);
|
||||
_errorMessage = string.Join(", ", errors);
|
||||
},
|
||||
unauthorized => { _errorMessage = "Invalid credentials."; },
|
||||
forbidden => { _errorMessage = "Access denied."; },
|
||||
error => { _errorMessage = error.Message; }
|
||||
);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user