From 1080d3b4ad9005d291bbbbfff71ded065f22a7c1 Mon Sep 17 00:00:00 2001 From: Egamorf Date: Mon, 21 Apr 2025 00:35:36 +0200 Subject: [PATCH] Enable authentication --- .../Pages/User/Component.razor | 0 .../Pages/User/Components/Login.razor.cs | 22 ++++++++-- .../Pages/User/Gateways/AuthGateway.cs | 7 ++-- .../Pages/User/UserMenu.razor | 42 +++++++++++++++---- .../Pages/User/UserMenu.razor.cs | 10 ++++- .../Client/GameIdeas.BlazorApp/Program.cs | 3 +- .../CreateStaticResourceKey.cs | 2 + .../Controllers/UserController.cs | 4 +- .../GameIdeas.WebAPI/Files/GameIdeas.fr.json | 1 + .../Server/GameIdeas.WebAPI/Program.cs | 2 + 10 files changed, 75 insertions(+), 18 deletions(-) create mode 100644 src/GameIdeas/Client/GameIdeas.BlazorApp/Pages/User/Component.razor diff --git a/src/GameIdeas/Client/GameIdeas.BlazorApp/Pages/User/Component.razor b/src/GameIdeas/Client/GameIdeas.BlazorApp/Pages/User/Component.razor new file mode 100644 index 0000000..e69de29 diff --git a/src/GameIdeas/Client/GameIdeas.BlazorApp/Pages/User/Components/Login.razor.cs b/src/GameIdeas/Client/GameIdeas.BlazorApp/Pages/User/Components/Login.razor.cs index 6d73a76..6d8a405 100644 --- a/src/GameIdeas/Client/GameIdeas.BlazorApp/Pages/User/Components/Login.razor.cs +++ b/src/GameIdeas/Client/GameIdeas.BlazorApp/Pages/User/Components/Login.razor.cs @@ -1,10 +1,14 @@ +using GameIdeas.BlazorApp.Pages.User.Gateways; using GameIdeas.Shared.Dto; +using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Forms; namespace GameIdeas.BlazorApp.Pages.User.Components; public partial class Login { + [Parameter] public IAuthGateway AuthGateway { get; set; } = default!; + private EditContext? EditContext; private UserDto UserDto = new(); private bool IsLoading = false; @@ -21,9 +25,19 @@ public partial class Login return; } - IsLoading = true; - await Task.Delay(TimeSpan.FromSeconds(5)); - IsLoading = false; + try + { + IsLoading = true; + await AuthGateway.Login(UserDto); + } + catch (Exception) + { + throw; + } + finally + { + IsLoading = false; + StateHasChanged(); + } } - } \ No newline at end of file diff --git a/src/GameIdeas/Client/GameIdeas.BlazorApp/Pages/User/Gateways/AuthGateway.cs b/src/GameIdeas/Client/GameIdeas.BlazorApp/Pages/User/Gateways/AuthGateway.cs index f19366e..ea2de5c 100644 --- a/src/GameIdeas/Client/GameIdeas.BlazorApp/Pages/User/Gateways/AuthGateway.cs +++ b/src/GameIdeas/Client/GameIdeas.BlazorApp/Pages/User/Gateways/AuthGateway.cs @@ -3,18 +3,19 @@ using GameIdeas.BlazorApp.Shared.Constants; using GameIdeas.BlazorApp.Shared.Exceptions; using GameIdeas.Resources; using GameIdeas.Shared.Dto; +using Microsoft.AspNetCore.Components.Authorization; namespace GameIdeas.BlazorApp.Pages.User.Gateways; public class AuthGateway(IHttpClientService httpClient, - JwtAuthenticationStateProvider stateProvider) : IAuthGateway + AuthenticationStateProvider stateProvider) : IAuthGateway { public async Task Login(UserDto userDto) { try { var token = await httpClient.PostAsync(Endpoints.Auth.Login, userDto); - await stateProvider.NotifyUserAuthenticationAsync(token!.Token!); + await ((JwtAuthenticationStateProvider)stateProvider).NotifyUserAuthenticationAsync(token!.Token!); return true; } catch (Exception) @@ -27,7 +28,7 @@ public class AuthGateway(IHttpClientService httpClient, { try { - await stateProvider.NotifyUserLogoutAsync(); + await ((JwtAuthenticationStateProvider)stateProvider).NotifyUserLogoutAsync(); } catch (Exception) { diff --git a/src/GameIdeas/Client/GameIdeas.BlazorApp/Pages/User/UserMenu.razor b/src/GameIdeas/Client/GameIdeas.BlazorApp/Pages/User/UserMenu.razor index cd46546..b069564 100644 --- a/src/GameIdeas/Client/GameIdeas.BlazorApp/Pages/User/UserMenu.razor +++ b/src/GameIdeas/Client/GameIdeas.BlazorApp/Pages/User/UserMenu.razor @@ -1,5 +1,8 @@ -@using GameIdeas.BlazorApp.Shared.Components.BackdropFilter +@using GameIdeas.BlazorApp.Pages.User.Components +@using GameIdeas.BlazorApp.Shared.Components.BackdropFilter @using GameIdeas.BlazorApp.Shared.Constants +@using GameIdeas.Shared.Constants +@using Microsoft.AspNetCore.Components.Authorization