From 51f86c6ef09df36f857c8d01f984364c74496c60 Mon Sep 17 00:00:00 2001 From: Thomas Andersen Date: Fri, 1 Nov 2024 12:51:28 +0100 Subject: [PATCH] Respect authentication attributes (#46) * Minor fix so the app now respecs the auth attributes, instead of only rendering login-page when un-authenticated * Inherit color on login page * Redirect to home on authentication --------- Co-authored-by: A-Guldborg --- Shifty.App/App.razor | 14 +++- Shifty.App/Components/RedirectToLogin.razor | 8 ++ Shifty.App/Pages/Login.razor | 88 ++++++++++++--------- Shifty.App/Shared/MainLayout.razor | 34 +++----- Shifty.App/_Imports.razor | 4 + 5 files changed, 84 insertions(+), 64 deletions(-) create mode 100644 Shifty.App/Components/RedirectToLogin.razor diff --git a/Shifty.App/App.razor b/Shifty.App/App.razor index 9287ef6..c0173c6 100644 --- a/Shifty.App/App.razor +++ b/Shifty.App/App.razor @@ -1,7 +1,17 @@ - +@using Shifty.App.Components +@using Shifty.App.Pages + - + + + + @if (routeData.PageType != typeof(Login)) + { + + } + + diff --git a/Shifty.App/Components/RedirectToLogin.razor b/Shifty.App/Components/RedirectToLogin.razor new file mode 100644 index 0000000..fd85df9 --- /dev/null +++ b/Shifty.App/Components/RedirectToLogin.razor @@ -0,0 +1,8 @@ +@inject NavigationManager Navigation + +@code { + protected override void OnInitialized() + { + Navigation.NavigateTo("/login", false); + } +} \ No newline at end of file diff --git a/Shifty.App/Pages/Login.razor b/Shifty.App/Pages/Login.razor index 5703d2f..c2256e7 100644 --- a/Shifty.App/Pages/Login.razor +++ b/Shifty.App/Pages/Login.razor @@ -1,48 +1,54 @@ @page "/Login" -@using Microsoft.AspNetCore.Components @using System.ComponentModel.DataAnnotations @using Shifty.App.Services @inject IAuthenticationService _authenticationService +@inject NavigationManager Navigation - - - - - @if (_loggingIn) - { - - } - else - { - if (!_successfulLogin) - { - Invalid credentials - } - - - - } - - - - Login - - - - +@attribute [AllowAnonymous] + + + Please login + + + + + @if (_loggingIn) + { + + } + else + { + if (!_successfulLogin) + { + Invalid credentials + } + + + + } + + + + Login + + + + + + @code { bool _loggingIn = false; @@ -65,5 +71,9 @@ _loggingIn = true; _successfulLogin = await _authenticationService.LoginUser(_loginForm.Email, _loginForm.Password); _loggingIn = false; + if (_successfulLogin) + { + Navigation.NavigateTo("/"); + } } } \ No newline at end of file diff --git a/Shifty.App/Shared/MainLayout.razor b/Shifty.App/Shared/MainLayout.razor index c9ab966..189eba2 100644 --- a/Shifty.App/Shared/MainLayout.razor +++ b/Shifty.App/Shared/MainLayout.razor @@ -1,4 +1,3 @@ -@using Shifty.App.Pages @using MudBlazor.Utilities @inherits LayoutComponentBase @@ -9,34 +8,27 @@ + + + + + + + Analog Shifty + - - - Analog Shifty - Shifty - - @Body - - - - - - - Please login - - - - - + + @Body + @code { @@ -59,10 +51,6 @@ } }; - protected override void OnInitialized() - { - } - void DrawerToggle() { _drawerOpen = !_drawerOpen; diff --git a/Shifty.App/_Imports.razor b/Shifty.App/_Imports.razor index 6859381..56cebf9 100644 --- a/Shifty.App/_Imports.razor +++ b/Shifty.App/_Imports.razor @@ -1,5 +1,6 @@ @using System.Net.Http @using System.Net.Http.Json +@using Microsoft.AspNetCore.Authorization @using Microsoft.AspNetCore.Components.Authorization @using Microsoft.AspNetCore.Components.Forms @using Microsoft.AspNetCore.Components.Routing @@ -11,3 +12,6 @@ @using Shifty.App.Shared @using MudBlazor + + +@attribute [Authorize] \ No newline at end of file