New source control repo for Biskilog POS - secure hub to store & manage source code. Streamlines dev process, tracks changes, & improves collaboration. Ensures reliable software.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
3.8 KiB

using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components;
using System.Net.Http.Headers;
using Biskilog_Accounting.Shared.ClientContractModels;
using Biskilog_Accounting.Shared.Interfaces;
using System.Net.Http.Json;
namespace Biskilog_Accounting.Client.Pages.Auth
{
public partial class Login
{
private string m_email, m_password;
private bool showUsernameError, showPasswordError;
private bool m_remember { get; set; }
protected bool IsVisible { get; set; }
//NotificationMessage notificationMessage = new NotificationMessage();
private Userauth authenticatedUser;
/// <summary>
/// Handles the click or press event of the enter key
/// </summary>
/// <param name="e"></param>
public async void Enter(KeyboardEventArgs e)
{
if (e.Code == "Enter" || e.Code == "NumpadEnter")
{
await pagaAuth();
}
}
/// <summary>
/// Authenticates the user and determines the type of page layout to show
/// </summary>
/// <returns></returns>
async Task pagaAuth()
{
ShowSpinner();
try
{
authenticatedUser = new Userauth
{
UserId = 0,
Username = m_email,
Email = m_email,
Passsword = m_password
};
var responseMain = await m_http.PostAsJsonAsync("api/authentication/type-a", authenticatedUser);
if (responseMain.IsSuccessStatusCode)
{
showUsernameError = string.IsNullOrEmpty(m_email);
showPasswordError = string.IsNullOrEmpty(m_password);
if(!showUsernameError && !showPasswordError)
{
string token = await responseMain.Content.ReadAsStringAsync();
await m_tokenService.SetToken(token, m_remember);
var authHeader = new AuthenticationHeaderValue("Bearer", token);
m_http.DefaultRequestHeaders.Authorization = authHeader;
m_navigationManager.NavigateTo("/");
}
}
else if (responseMain.StatusCode == System.Net.HttpStatusCode.BadRequest)
{
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
HideSpinner();
}
/// <summary>
/// Shows the loading spinner
/// </summary>
public void ShowSpinner()
{
IsVisible = true;
StateHasChanged();
}
/// <summary>
/// Hides the loading spinner
/// </summary>
public void HideSpinner()
{
IsVisible = false;
StateHasChanged();
}
/// <summary>
/// Shows a notification message
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
//async Task ShowNotification(NotificationMessage message)
//{
// notificationService.Notify(message);
// await InvokeAsync(() => { StateHasChanged(); });
//}
/// <summary>
/// Sets the username value
/// </summary>
/// <param name="value"></param>
void usernameInput(string value)
{
m_email = value;
StateHasChanged();
}
/// <summary>
/// Sets the password value
/// </summary>
/// <param name="value"></param>
void passwordInput(string value)
{
m_password = value;
StateHasChanged();
}
}
}