Sikeres POST az eszközről

This commit is contained in:
2025-01-24 11:15:17 +01:00
parent 862f0a148b
commit 0d44cd48fb
7 changed files with 82 additions and 18 deletions
+38 -1
View File
@@ -1,4 +1,5 @@
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using WorkFlowCheck.Common.DTO;
using WorkFlowCheck.MAUI.Services; using WorkFlowCheck.MAUI.Services;
namespace WorkFlowCheck.MAUI namespace WorkFlowCheck.MAUI
@@ -9,8 +10,44 @@ namespace WorkFlowCheck.MAUI
{ {
InitializeComponent(); InitializeComponent();
MainPage = serviceProvider.GetRequiredService<MainPage>(); //MainPage = serviceProvider.GetRequiredService<MainPage>();
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<ApiResponseDTO<UserDTO>> CheckSecurity()
{
var isLogged = await SecureStorage.Default.GetAsync("WFCUser");
if (isLogged == null)
{
return new ApiResponseDTO<UserDTO>()
{
IsSuccess = false
};
}
return new ApiResponseDTO<UserDTO>()
{
IsSuccess = true
};
}
} }
} }
+3 -4
View File
@@ -4,12 +4,11 @@
xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:WorkFlowCheck.MAUI" xmlns:local="clr-namespace:WorkFlowCheck.MAUI"
xmlns:security="clr-namespace:WorkFlowCheck.MAUI.Pages.Security"
Shell.FlyoutBehavior="Disabled" Shell.FlyoutBehavior="Disabled"
Title="WorkFlowCheck.MAUI"> Title="WorkFlowCheck.MAUI">
<ShellContent <ShellContent Title="Home" ContentTemplate="{DataTemplate local:MainPage}" Route="MainPage" />
Title="Home" <ShellContent Title="Login" ContentTemplate="{DataTemplate security:LoginPage}" Route="LoginPage" />
ContentTemplate="{DataTemplate local:MainPage}"
Route="MainPage" />
</Shell> </Shell>
@@ -1,3 +1,4 @@
using WorkFlowCheck.MAUI.Services; using WorkFlowCheck.MAUI.Services;
namespace WorkFlowCheck.MAUI.Pages.Security; namespace WorkFlowCheck.MAUI.Pages.Security;
@@ -5,17 +6,17 @@ namespace WorkFlowCheck.MAUI.Pages.Security;
public partial class LoginPage : ContentPage public partial class LoginPage : ContentPage
{ {
private readonly IUserService _userService; private readonly IUserService _userService;
public LoginPage(IUserService userService) public LoginPage()
{ {
InitializeComponent(); InitializeComponent();
_userService = userService; _userService = MauiProgram.ServiceProvider.GetRequiredService<IUserService>();
} }
private async void OnLoginClicked(object sender, EventArgs e) private async void OnLoginClicked(object sender, EventArgs e)
{ {
// Példa bejelentkezés // 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"); await DisplayAlert("Success", "You are now logged in.", "OK");
@@ -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)] [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 public class MainActivity : MauiAppCompatActivity
{ {
protected async override void OnDestroy()
{
SecureStorage.Default.Remove("WFCUser");
base.OnDestroy();
}
} }
} }
@@ -10,6 +10,6 @@ namespace WorkFlowCheck.MAUI.Services
public interface IUserService public interface IUserService
{ {
Task<ApiResponseDTO<UserDTO>> GetUserAsync(); Task<ApiResponseDTO<UserDTO>> GetUserAsync();
Task<IApiResponseDTO<UserDTO>> Login(string username, string password); Task<ApiResponseDTO<UserDTO>> Authenticate(string username, string password);
} }
} }
+29 -8
View File
@@ -1,11 +1,15 @@
using Microsoft.Extensions.Configuration; using System.Net.Http;
using Microsoft.Extensions.Configuration;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net.Http.Json; using System.Net.Http.Json;
using System.Text; using System.Text;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using WorkFlowCheck.Common.DTO; using WorkFlowCheck.Common.DTO;
using System.Text.Json.Serialization;
using Newtonsoft.Json;
namespace WorkFlowCheck.MAUI.Services namespace WorkFlowCheck.MAUI.Services
{ {
@@ -46,20 +50,37 @@ namespace WorkFlowCheck.MAUI.Services
}; };
} }
} }
public async Task<ApiResponseDTO<UserDTO>> Login(string username, string password) public async Task<ApiResponseDTO<UserDTO>> Authenticate(string username, string password)
{ {
try try
{ {
// Az API végpont meghatározása // 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 userDTO = new UserDTO()
var response = await _httpClient.GetFromJsonAsync<ApiResponseDTO<UserDTO>>(endpoint);
return response ?? new ApiResponseDTO<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<ApiResponseDTO<UserDTO>>(jsonString);
return response ?? new ApiResponseDTO<UserDTO>
{
IsSuccess = false,
};
}
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -64,6 +64,7 @@
<PackageReference Include="Microsoft.Maui.Controls" Version="$(MauiVersion)" /> <PackageReference Include="Microsoft.Maui.Controls" Version="$(MauiVersion)" />
<PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="$(MauiVersion)" /> <PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="$(MauiVersion)" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.1" /> <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>