@page "/login" @using BlazorFrontend.Services @using BlazorFrontend.Providers @using LibraryManagement.Shared.Models @using Microsoft.AspNetCore.Components.Authorization @inject HttpClient Http @inject AuthenticationStateProvider AuthStateProvider @inject NavigationManager Navigation Sign In

Welcome back

Continue where you left off.

@if (!string.IsNullOrEmpty(_errorMessage)) {
@_errorMessage
}
Don't have an account? Create one for free
@code { [SupplyParameterFromForm] private LoginRequest _loginRequest { get; set; } = new(); private string? _errorMessage; private bool _isLoading; private async Task HandleLogin() { try { _isLoading = true; _errorMessage = null; var res = await Http.PostAsJsonAsync("api/auth/login", _loginRequest); if (res.IsSuccessStatusCode) { var response = await res.Content.ReadFromJsonAsync(); if (response != null && !string.IsNullOrEmpty(response.Token)) { var jwtProvider = (JwtAuthenticationStateProvider)AuthStateProvider; await jwtProvider.LoginAsync(response.Token); // ensure storage is ready await Task.Delay(100); Navigation.NavigateTo("/"); return; } } _errorMessage = "Invalid credentials or server error."; } catch (Exception ex) { _errorMessage = "An error occurred: " + ex.Message; } finally { _isLoading = false; } } }