Files
StandManager/StandManager/Components/Pages/Management/Login.razor
2025-12-01 09:56:00 +01:00

85 lines
3.0 KiB
Plaintext

@rendermode InteractiveServer
@layout PublicLayout
@page "/management/login"
@using System.Security.Claims
@using Microsoft.AspNetCore.Authentication
@using Microsoft.AspNetCore.Identity
@using StandManager.Components.Layout
@using StandManager.Domain.Entita
@using StandManager.Model
@using StandManager.Service.Interfaces
@inject NavigationManager Nav
@inject IManagerService _managerService
<div class="page page-center">
<div class="container container-tight py-4">
<div class="text-center mb-4">
</div>
<div class="card card-md">
<div class="card-body">
<h2 class="h2 text-center mb-4">Accedi</h2>
<EditForm Model="model" OnValidSubmit="HandleValidSubmit" FormName="loginForm">
<DataAnnotationsValidator/>
<div class="mb-3">
<label class="form-label">Email</label>
<InputText class="form-control" @bind-Value="model.Email"/>
<ValidationMessage For="@(() => model.Email)"/>
</div>
<div class="mb-3">
<label class="form-label">Password</label>
<InputText class="form-control" @bind-Value="model.Password" type="password"/>
<ValidationMessage For="@(() => model.Password)"/>
</div>
<button type="submit" class="btn btn-primary w-100">
Accedi
</button>
@if (!string.IsNullOrEmpty(errorMessage))
{
<div class="alert alert-danger mt-3">@errorMessage</div>
}
</EditForm>
</div>
</div>
</div>
</div>
@code {
[CascadingParameter] public HttpContext httpContext { get; set; } = default;
[SupplyParameterFromForm]
private LoginModel model { get; set; }
private string? errorMessage;
protected override void OnInitialized() => model ??= new();
private async Task HandleValidSubmit()
{
errorMessage = null;
var user = await _managerService.UtenteService.RicercaPer(x => x.Email == model.Email);
var hasher = new PasswordHasher<Utente>();
if (user == null || hasher.VerifyHashedPassword(user, user.Password, model.Password) != PasswordVerificationResult.Success)
{
errorMessage = "Credenziali non valide.";
return;
}
List<Claim> claims =
[
new(ClaimTypes.Name, user.Email),
new(ClaimTypes.GivenName, user.Nome),
new("Id", user.Id.ToString())
];
ClaimsIdentity identity = new(claims, "standmanager");
ClaimsPrincipal claimsPrincipal = new(identity);
await httpContext.SignInAsync("standmanager", claimsPrincipal, new AuthenticationProperties
{
IsPersistent = true,
ExpiresUtc = DateTime.UtcNow.AddHours(8)
});
Nav.NavigateTo("/management");
}
}