Skip to content

Commit

Permalink
Respect authentication attributes (#46)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
TTA777 and A-Guldborg authored Nov 1, 2024
1 parent 181b0bb commit 51f86c6
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 64 deletions.
14 changes: 12 additions & 2 deletions Shifty.App/App.razor
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
<CascadingAuthenticationState>
@using Shifty.App.Components
@using Shifty.App.Pages
<CascadingAuthenticationState>
<Router AppAssembly="@typeof(Program).Assembly" PreferExactMatches="@true">
<Found Context="routeData">
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)"/>
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
<NotAuthorized>
<!-- Redirect unauthenticated users to login -->
@if (routeData.PageType != typeof(Login))
{
<RedirectToLogin />
}
</NotAuthorized>
</AuthorizeRouteView>
</Found>
<NotFound>
<LayoutView Layout="@typeof(MainLayout)">
Expand Down
8 changes: 8 additions & 0 deletions Shifty.App/Components/RedirectToLogin.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@inject NavigationManager Navigation

@code {
protected override void OnInitialized()
{
Navigation.NavigateTo("/login", false);
}
}
88 changes: 49 additions & 39 deletions Shifty.App/Pages/Login.razor
Original file line number Diff line number Diff line change
@@ -1,48 +1,54 @@
@page "/Login"
@using Microsoft.AspNetCore.Components
@using System.ComponentModel.DataAnnotations
@using Shifty.App.Services
@inject IAuthenticationService _authenticationService
@inject NavigationManager Navigation

<EditForm Model="@_loginForm" OnValidSubmit="async () => await LoginUser()" >
<DataAnnotationsValidator/>
<MudCard Class="mb-auto">
<MudCardContent>
@if (_loggingIn)
{
<MudProgressCircular Color="Color.Default" Indeterminate="true" />
}
else
{
if (!_successfulLogin)
{
<MudAlert Severity="Severity.Warning" ShowCloseIcon="true">Invalid credentials</MudAlert>
}

<MudTextField T="string"
@bind-Value="_loginForm.Email"
Label="Email"
For="() => _loginForm.Email"
Immediate="true"
DebounceInterval="500"/>
<MudTextField T="string" Label="Password"
@bind-Value="_loginForm.Password"
InputType="InputType.Password"
For="() => _loginForm.Password"
Immediate="true"/>
}
</MudCardContent>
<MudCardActions>
<MudButton ButtonType="ButtonType.Submit"
Variant="Variant.Filled"
Color="Color.Primary"
Class="ml-auto">
Login
</MudButton>
</MudCardActions>
</MudCard>
</EditForm>
@attribute [AllowAnonymous]

<MudPaper Elevation="0" Height="60vh" Style="@($"background: {Color.Inherit}")" Class="d-flex justify-center align-center">
<MudPaper Width="40vw">
<MudAlert Severity="Severity.Info">Please login</MudAlert>
<EditForm Model="@_loginForm" OnValidSubmit="async () => await LoginUser()" >
<DataAnnotationsValidator/>
<MudCard Class="mb-auto">
<MudCardContent>
@if (_loggingIn)
{
<MudProgressCircular Color="Color.Default" Indeterminate="true" />
}
else
{
if (!_successfulLogin)
{
<MudAlert Severity="Severity.Warning" ShowCloseIcon="true">Invalid credentials</MudAlert>
}

<MudTextField T="string"
@bind-Value="_loginForm.Email"
Label="Email"
For="() => _loginForm.Email"
Immediate="true"
DebounceInterval="500"/>
<MudTextField T="string" Label="Password"
@bind-Value="_loginForm.Password"
InputType="InputType.Password"
For="() => _loginForm.Password"
Immediate="true"/>
}
</MudCardContent>
<MudCardActions>
<MudButton ButtonType="ButtonType.Submit"
Variant="Variant.Filled"
Color="Color.Primary"
Class="ml-auto">
Login
</MudButton>
</MudCardActions>
</MudCard>
</EditForm>
</MudPaper>
</MudPaper>

@code {
bool _loggingIn = false;
Expand All @@ -65,5 +71,9 @@
_loggingIn = true;
_successfulLogin = await _authenticationService.LoginUser(_loginForm.Email, _loginForm.Password);
_loggingIn = false;
if (_successfulLogin)
{
Navigation.NavigateTo("/");
}
}
}
34 changes: 11 additions & 23 deletions Shifty.App/Shared/MainLayout.razor
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
@using Shifty.App.Pages
@using MudBlazor.Utilities
@inherits LayoutComponentBase

Expand All @@ -9,34 +8,27 @@
<MudSnackbarProvider />

<MudLayout>
<MudAppBar Elevation="0">
<AuthorizeView>
<Authorized>
<MudIconButton Icon="@Icons.Material.Filled.Menu" Color="Color.Inherit" Edge="Edge.Start" OnClick="@((_) => DrawerToggle())"/>
</Authorized>
</AuthorizeView>
<MudText Typo="Typo.h6" Align="Align.Left">Analog Shifty</MudText>
</MudAppBar>
<AuthorizeView>
<Authorized>
<MudAppBar Elevation="0">
<MudIconButton Icon="@Icons.Material.Filled.Menu" Color="Color.Inherit" Edge="Edge.Start" OnClick="@((_) => DrawerToggle())"/>
<MudText Typo="Typo.h6" Align="Align.Left">Analog Shifty</MudText>
</MudAppBar>
<MudDrawer @bind-Open="_drawerOpen" Elevation="1">
<MudDrawerHeader>
<MudText Typo="Typo.h6">Shifty</MudText>
</MudDrawerHeader>
<NavMenu/>
</MudDrawer>
<MudMainContent>
@Body
</MudMainContent>
</Authorized>
<NotAuthorized>
<MudAppBar></MudAppBar>
<MudMainContent>
<MudPaper Elevation="0" Height="60vh" Class="d-flex justify-center align-center" Style=@($"background:{_theme.Palette.Background}")>
<MudPaper Width="40vw">
<MudAlert Severity="Severity.Info">Please login</MudAlert>
<Login/>
</MudPaper>
</MudPaper>
</MudMainContent>
</NotAuthorized>
</AuthorizeView>
<MudMainContent>
@Body
</MudMainContent>
</MudLayout>

@code {
Expand All @@ -59,10 +51,6 @@
}
};

protected override void OnInitialized()
{
}

void DrawerToggle()
{
_drawerOpen = !_drawerOpen;
Expand Down
4 changes: 4 additions & 0 deletions Shifty.App/_Imports.razor
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -11,3 +12,6 @@
@using Shifty.App.Shared

@using MudBlazor


@attribute [Authorize]

0 comments on commit 51f86c6

Please sign in to comment.