feat(frontend): Login and logout now properly work

This commit is contained in:
Samuele Lorefice
2025-07-17 15:08:50 +02:00
parent f67af97c3f
commit 0ee27a4260
4 changed files with 33 additions and 37 deletions

View File

@@ -1,4 +1,5 @@
@using MilkStream.Services
@using Butter.Dtos.User
@using MilkStream.Services
@using Butter.Types
@inherits LayoutComponentBase
@@ -30,7 +31,7 @@
</li>
</ul>
@if (isConnected) {
if (!loggedIn) {
if (!LoggedIn) {
<button class="btn btn-primary mx-1" @onclick="OnLoginClick">
Login
</button>
@@ -48,14 +49,14 @@
</button>
</div>
if (admin) {
if (Admin) {
<button class="btn btn-outline-info mx-1" @onclick="OnSettingsClick">
<i class="bi bi-gear-wide-connected"></i>
Settings
</button>
}
<div class="mx-2">@username</div>
<div class="mx-2">@userInfo?.Username</div>
<div class="dropdown">
<button class="btn btn-outline-light dropdown-toggle" type="button" data-bs-toggle="dropdown">
<i class="bi bi-person-circle"></i>
@@ -71,11 +72,12 @@
</div>
@code{
public bool LoggedIn => userInfo != null;
bool Admin => userInfo?.AccessLevel == EAccessLevel.Admin;
bool isConnected;
bool loggedIn;
bool admin;
bool needsUpdate;
string username = string.Empty;
UserInfoDto? userInfo;
protected async override Task OnAfterRenderAsync(bool firstRender) {
if (firstRender || needsUpdate) {
@@ -84,47 +86,30 @@
needsUpdate = false;
StateHasChanged();
}
}
private async Task LoadStateAsync() {
var auth = await localStorage.GetAsync<AuthInfo>("auth");
if (auth.Success == false) return;
loginService.AuthInfo = auth.Value;
var userInfo = await userService.GetUserAsync().ConfigureAwait(false);
if (userInfo == null) {
loggedIn = false;
return;
}
loggedIn = true;
username = userInfo.Username;
admin = userInfo.AccessLevel == EAccessLevel.Admin;
userInfo = await userService.GetUserAsync().ConfigureAwait(false);
}
async Task OnLoginClick() {
var userInfo = await userService.GetUserAsync();
userInfo = await userService.GetUserAsync();
if (userInfo != null) {
loggedIn = true;
username = userInfo.Username;
admin = userInfo.AccessLevel == EAccessLevel.Admin;
return;
}
navigation.NavigateTo("/login");
if (userInfo != null) needsUpdate = true;
else navigation.NavigateTo("/login");
}
void OnRegisterClick() => navigation.NavigateTo("/register");
void OnSettingsClick() => navigation.NavigateTo("/settings");
void OnLogout() {
async Task OnLogout() {
_ = loginService.Logout();
needsUpdate = true;
StateHasChanged();
navigation.NavigateTo("/login");
await localStorage.DeleteAsync("auth");
navigation.NavigateTo("/", true);
}
}

View File

@@ -1,5 +1,6 @@
@page "/"
@using Butter.Dtos;
@using MilkStream.Components.Layout
@using MilkStream.Services
@inject NavigationManager navigationManager
@@ -16,7 +17,7 @@
@if (mediaList.Count == 0) {
<div class="border-warning border-5 border-opacity-100 rounded-3 p-3 m-5 bg-warning-subtle text-center">
<h2 class="text-warning">No media available</h2>
@if (loginService.IsLoggedIn) {
@if (NavMenu is { LoggedIn: true }) {
<p class="text-warning-emphasis">You are logged in, but there is no media available at the moment.</p>
} else {
<p class="text-warning-emphasis">Please <a href="/login" class="link-warning">log-in</a> to be able to browse any media.</p>
@@ -35,6 +36,10 @@
</div>
@code{
[Parameter]
public NavMenu? NavMenu { get; set; }
bool isLoading = true;
List<MediaDto> mediaList = new();

View File

@@ -1,5 +1,6 @@
@page "/login"
@using Butter.Dtos
@using Microsoft.AspNetCore.Mvc.RazorPages
@using MilkStream.Services
@inject NavigationManager navigation
@@ -32,6 +33,10 @@
string password = string.Empty;
bool isLoginFailed;
protected override void OnInitialized() {
//if (loginService.IsLoggedIn) navigation.NavigateTo("/", true);
}
void Register_OnClick() => navigation.NavigateTo("register");
async Task Login_OnClick() {
@@ -40,7 +45,7 @@
if (result.Item1) { // Login successful
isLoginFailed = false;
await protectedLocalStorage.SetAsync("auth", result.Item2!);
navigation.NavigateTo("/"); // Redirect to home page
navigation.NavigateTo("/", true); // Redirect to home page
} else {
isLoginFailed = true;
}

View File

@@ -57,4 +57,5 @@
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AUtf8JsonWriterCache_002Ecs_002Fl_003AC_0021_003FUsers_003FREDCODE_003FAppData_003FRoaming_003FJetBrains_003FRider2025_002E2_003Fresharper_002Dhost_003FSourcesCache_003F96d87fca98b4167c66ffae61c9ee88dc182d6e7dbc7eb8dbf41297e660eb_003FUtf8JsonWriterCache_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AValueTask_002Ecs_002Fl_003AC_0021_003FUsers_003FREDCODE_003FAppData_003FRoaming_003FJetBrains_003FRider2025_002E2_003Fresharper_002Dhost_003FSourcesCache_003F9ec7c6b688aa4b89a1477dc1b679f62bf856b50196f4b8d19cd77f86df0abc_003FValueTask_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AWriteStackFrame_002Ecs_002Fl_003AC_0021_003FUsers_003FREDCODE_003FAppData_003FRoaming_003FJetBrains_003FRider2025_002E2_003Fresharper_002Dhost_003FSourcesCache_003Fcfb06811acbbb25f4c1f3ae4eee74f65da5b2e9bd1bdc01ad979ac9b6745e9_003FWriteStackFrame_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpCodeStyle/ACCESSOR_OWNER_BODY/@EntryValue">ExpressionBody</s:String>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpCodeStyle/ThisQualifier/INSTANCE_MEMBERS_QUALIFY_DECLARED_IN/@EntryValue">0</s:String></wpf:ResourceDictionary>