From 0d44cd48fbd9efb310bdf8aac8d3ad8322b44663 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szab=C3=B3=20Iv=C3=A1n?= Date: Fri, 24 Jan 2025 11:15:17 +0100 Subject: [PATCH] =?UTF-8?q?Sikeres=20POST=20az=20eszk=C3=B6zr=C5=91l?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/WorkFlowCheck.MAUI/App.xaml.cs | 39 ++++++++++++++++++- src/WorkFlowCheck.MAUI/AppShell.xaml | 7 ++-- .../Pages/Security/LoginPage.xaml.cs | 9 +++-- .../Platforms/Android/MainActivity.cs | 5 +++ .../Services/IUserService.cs | 2 +- .../Services/UserService.cs | 37 ++++++++++++++---- .../WorkFlowCheck.MAUI.csproj | 1 + 7 files changed, 82 insertions(+), 18 deletions(-) diff --git a/src/WorkFlowCheck.MAUI/App.xaml.cs b/src/WorkFlowCheck.MAUI/App.xaml.cs index 6f3241b..16d827f 100644 --- a/src/WorkFlowCheck.MAUI/App.xaml.cs +++ b/src/WorkFlowCheck.MAUI/App.xaml.cs @@ -1,4 +1,5 @@ using Microsoft.Extensions.DependencyInjection; +using WorkFlowCheck.Common.DTO; using WorkFlowCheck.MAUI.Services; namespace WorkFlowCheck.MAUI @@ -9,8 +10,44 @@ namespace WorkFlowCheck.MAUI { InitializeComponent(); - MainPage = serviceProvider.GetRequiredService(); + //MainPage = serviceProvider.GetRequiredService(); + MainPage = new AppShell(); } + protected async override void OnResume() + { + base.OnResume(); + var isLogged = await SecureStorage.Default.GetAsync("WFCUser"); + if (isLogged == null) + { + + } + } + protected override void OnSleep() + { + SecureStorage.Default.Remove("WFCUser"); + base.OnSleep(); + } + protected async override void OnStart() + { + base.OnStart(); + SecureStorage.Default.Remove("WFCUser"); + await Shell.Current.GoToAsync("//LoginPage"); + } + private async Task> CheckSecurity() + { + var isLogged = await SecureStorage.Default.GetAsync("WFCUser"); + if (isLogged == null) + { + return new ApiResponseDTO() + { + IsSuccess = false + }; + } + return new ApiResponseDTO() + { + IsSuccess = true + }; + } } } diff --git a/src/WorkFlowCheck.MAUI/AppShell.xaml b/src/WorkFlowCheck.MAUI/AppShell.xaml index b26fd5d..a6ab65e 100644 --- a/src/WorkFlowCheck.MAUI/AppShell.xaml +++ b/src/WorkFlowCheck.MAUI/AppShell.xaml @@ -4,12 +4,11 @@ xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:WorkFlowCheck.MAUI" + xmlns:security="clr-namespace:WorkFlowCheck.MAUI.Pages.Security" Shell.FlyoutBehavior="Disabled" Title="WorkFlowCheck.MAUI"> - + + diff --git a/src/WorkFlowCheck.MAUI/Pages/Security/LoginPage.xaml.cs b/src/WorkFlowCheck.MAUI/Pages/Security/LoginPage.xaml.cs index 975e99e..92ec965 100644 --- a/src/WorkFlowCheck.MAUI/Pages/Security/LoginPage.xaml.cs +++ b/src/WorkFlowCheck.MAUI/Pages/Security/LoginPage.xaml.cs @@ -1,3 +1,4 @@ + using WorkFlowCheck.MAUI.Services; namespace WorkFlowCheck.MAUI.Pages.Security; @@ -5,17 +6,17 @@ namespace WorkFlowCheck.MAUI.Pages.Security; public partial class LoginPage : ContentPage { private readonly IUserService _userService; - public LoginPage(IUserService userService) + public LoginPage() { InitializeComponent(); - _userService = userService; + _userService = MauiProgram.ServiceProvider.GetRequiredService(); } private async void OnLoginClicked(object sender, EventArgs e) { // Példa bejelentkezés - bool success = _userService.Login(UsernameEntry.Text, PasswordEntry.Text); + var response = await _userService.Authenticate(UsernameEntry.Text.ToString(), PasswordEntry.Text.ToString()); - if (success) + if (response != null && response.IsSuccess) { await DisplayAlert("Success", "You are now logged in.", "OK"); diff --git a/src/WorkFlowCheck.MAUI/Platforms/Android/MainActivity.cs b/src/WorkFlowCheck.MAUI/Platforms/Android/MainActivity.cs index c62092b..8bf9c71 100644 --- a/src/WorkFlowCheck.MAUI/Platforms/Android/MainActivity.cs +++ b/src/WorkFlowCheck.MAUI/Platforms/Android/MainActivity.cs @@ -7,5 +7,10 @@ namespace WorkFlowCheck.MAUI [Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, LaunchMode = LaunchMode.SingleTop, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)] public class MainActivity : MauiAppCompatActivity { + protected async override void OnDestroy() + { + SecureStorage.Default.Remove("WFCUser"); + base.OnDestroy(); + } } } diff --git a/src/WorkFlowCheck.MAUI/Services/IUserService.cs b/src/WorkFlowCheck.MAUI/Services/IUserService.cs index 310400e..3a51074 100644 --- a/src/WorkFlowCheck.MAUI/Services/IUserService.cs +++ b/src/WorkFlowCheck.MAUI/Services/IUserService.cs @@ -10,6 +10,6 @@ namespace WorkFlowCheck.MAUI.Services public interface IUserService { Task> GetUserAsync(); - Task> Login(string username, string password); + Task> Authenticate(string username, string password); } } diff --git a/src/WorkFlowCheck.MAUI/Services/UserService.cs b/src/WorkFlowCheck.MAUI/Services/UserService.cs index dedeb65..0245763 100644 --- a/src/WorkFlowCheck.MAUI/Services/UserService.cs +++ b/src/WorkFlowCheck.MAUI/Services/UserService.cs @@ -1,11 +1,15 @@ -using Microsoft.Extensions.Configuration; +using System.Net.Http; +using Microsoft.Extensions.Configuration; using System; using System.Collections.Generic; using System.Linq; using System.Net.Http.Json; using System.Text; +using System.Threading; using System.Threading.Tasks; using WorkFlowCheck.Common.DTO; +using System.Text.Json.Serialization; +using Newtonsoft.Json; namespace WorkFlowCheck.MAUI.Services { @@ -46,20 +50,37 @@ namespace WorkFlowCheck.MAUI.Services }; } } - public async Task> Login(string username, string password) + public async Task> Authenticate(string username, string password) { try { // Az API végpont meghatározása - string endpoint = $"{_httpClient.BaseAddress}api/User/Login"; + string endpoint = $"{_httpClient.BaseAddress}api/user/authenticate"; - // HTTP GET kérés küldése - var response = await _httpClient.GetFromJsonAsync>(endpoint); - - return response ?? new ApiResponseDTO + var userDTO = new UserDTO() { - IsSuccess = false, + Email = "", + FirstName = "", + LastName = "", + Id = 0, + UserName = username, + Password = password, }; + + // HTTP POST kérés küldése + + using (HttpResponseMessage httpResponseMessage = await _httpClient.PostAsJsonAsync(endpoint, userDTO)) + { + httpResponseMessage.EnsureSuccessStatusCode(); + + var jsonString = await httpResponseMessage.Content.ReadAsStringAsync(); + var response = JsonConvert.DeserializeObject>(jsonString); + + return response ?? new ApiResponseDTO + { + IsSuccess = false, + }; + } } catch (Exception ex) { diff --git a/src/WorkFlowCheck.MAUI/WorkFlowCheck.MAUI.csproj b/src/WorkFlowCheck.MAUI/WorkFlowCheck.MAUI.csproj index 388e4c0..5a3df93 100644 --- a/src/WorkFlowCheck.MAUI/WorkFlowCheck.MAUI.csproj +++ b/src/WorkFlowCheck.MAUI/WorkFlowCheck.MAUI.csproj @@ -64,6 +64,7 @@ +