Files
mesa-fab-approval/MesaFabApproval.Client/Pages/Login.razor.cs
2025-06-04 10:26:48 -07:00

54 lines
1.7 KiB
C#

using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.WebUtilities;
using Microsoft.Extensions.Caching.Memory;
using MudBlazor;
namespace MesaFabApproval.Client.Pages;
public partial class Login {
[Inject] NavigationManager navigationManager { get; set; }
[Inject] IMemoryCache cache { get; set; }
public string? _redirectPath { get; set; }
private bool success;
private bool processing = false;
private string[] errors = { };
private string? username;
private string? password;
protected override async Task OnParametersSetAsync() {
Uri uri = navigationManager.ToAbsoluteUri(navigationManager.Uri);
if (QueryHelpers.ParseQuery(uri.Query).TryGetValue("redirectPath", out var redirectPath)) {
_redirectPath = System.Net.WebUtility.UrlDecode(redirectPath);
}
if (string.IsNullOrWhiteSpace(_redirectPath)) {
_redirectPath = cache.Get<string>("redirectUrl");
}
}
private async Task SubmitLogin() {
processing = true;
if (string.IsNullOrWhiteSpace(username)) snackbar.Add("Username is required!", Severity.Error);
else if (string.IsNullOrWhiteSpace(password)) snackbar.Add("Password is required!", Severity.Error);
else {
await authStateProvider.LoginAsync(username, password);
if (!string.IsNullOrWhiteSpace(_redirectPath)) {
navManager.NavigateTo(_redirectPath);
} else {
navManager.NavigateTo("dashboard");
}
}
processing = false;
}
private async Task SubmitIfEnter(KeyboardEventArgs e) {
if (e.Key == "Enter" && success) {
SubmitLogin();
}
}
}