From 62fa6c626c79b85b04b6dd66d59f68b9fddc891b Mon Sep 17 00:00:00 2001 From: Nana Antwi Date: Sat, 27 May 2023 16:58:05 +0000 Subject: [PATCH] Added Login authentication --- Client/Pages/Auth/Login.razor | 17 ++++++++++++--- Client/Pages/Auth/Login.razor.cs | 27 +++++++++++++++++++++++- Server/Services/AuthenticationService.cs | 5 ++++- Shared/ClientContractModels/Userauth.cs | 6 ++++++ 4 files changed, 50 insertions(+), 5 deletions(-) diff --git a/Client/Pages/Auth/Login.razor b/Client/Pages/Auth/Login.razor index ec6e3c2..35248d3 100644 --- a/Client/Pages/Auth/Login.razor +++ b/Client/Pages/Auth/Login.razor @@ -1,5 +1,8 @@ @layout AuthLayout @page "/" +@inject HttpClient httpclient +@inject NavigationManager navigationmanager + + + +
@@ -75,20 +81,25 @@

Please login to your account

+ @if (!string.IsNullOrEmpty(errormessage)) + { +

@errormessage

+ }
+ placeholder="Phone number or email address" @bind-value="@m_userAuth.Username" />
- +
- diff --git a/Client/Pages/Auth/Login.razor.cs b/Client/Pages/Auth/Login.razor.cs index cc6edb0..74b10a1 100644 --- a/Client/Pages/Auth/Login.razor.cs +++ b/Client/Pages/Auth/Login.razor.cs @@ -1,7 +1,32 @@ -namespace Biskilog_Accounting.Client.Pages.Auth +using System.Net.Http.Json; +using Biskilog_Accounting.Shared.ClientContractModels; + +namespace Biskilog_Accounting.Client.Pages.Auth { public partial class Login { + private Userauth m_userAuth; + private string errormessage; + + protected override void OnInitialized() + { + m_userAuth = new(); + base.OnInitialized(); + } + public async Task AuthUser() + { + var response = await httpclient.PostAsJsonAsync("api/authentication", m_userAuth); + if (response.IsSuccessStatusCode) + { + navigationmanager.NavigateTo("/welcome"); + } + else + { + errormessage = "Invalid Username or password"; + } + + } + } } diff --git a/Server/Services/AuthenticationService.cs b/Server/Services/AuthenticationService.cs index 172071b..321f325 100644 --- a/Server/Services/AuthenticationService.cs +++ b/Server/Services/AuthenticationService.cs @@ -7,10 +7,11 @@ namespace Biskilog_Accounting.Server.Services { public class AuthenticationService : IAuthService { + private readonly BiskilogContext m_context; private readonly ITokenService m_tokenService; - public AuthenticationService(BiskilogContext a_context, ITokenService a_tokenService) + public AuthenticationService( BiskilogContext a_context, ITokenService a_tokenService) { m_context = a_context; m_tokenService = a_tokenService; @@ -124,8 +125,10 @@ namespace Biskilog_Accounting.Server.Services // if (verified) // { + //TODO have a complete implementation return await m_context.Userauths.FirstAsync(u => u.Username == username && u.Passsword == password); + // } // else // { diff --git a/Shared/ClientContractModels/Userauth.cs b/Shared/ClientContractModels/Userauth.cs index d8b010a..44b0d6a 100644 --- a/Shared/ClientContractModels/Userauth.cs +++ b/Shared/ClientContractModels/Userauth.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; namespace Biskilog_Accounting.Shared.ClientContractModels; @@ -9,10 +10,15 @@ public partial class Userauth public int ClientId { get; set; } + [Required] + [Display(Name = "Username")] public string? Username { get; set; } public string? Email { get; set; } + [Required] + [DataType(DataType.Password)] + [Display(Name = "Password")] public string? Passsword { get; set; } public string? PhoneNumber { get; set; }