[frontend] Fix build warnings

This commit is contained in:
Laura Hausmann 2024-04-09 21:48:26 +02:00 committed by Lilian
parent 98ac934897
commit bc14abbacc
No known key found for this signature in database
GPG key ID: 007CA12D692829E1
4 changed files with 26 additions and 17 deletions

View file

@ -3,7 +3,7 @@ using Iceshrimp.Shared.Schemas;
namespace Iceshrimp.Frontend.Core.Schemas; namespace Iceshrimp.Frontend.Core.Schemas;
public class StoredUser() : UserResponse public class StoredUser : UserResponse
{ {
[JsonPropertyName("token")] public required string Token { get; set; } [JsonPropertyName("token")] public required string Token { get; set; }
} }

View file

@ -1,6 +1,5 @@
using Blazored.LocalStorage; using Blazored.LocalStorage;
using Iceshrimp.Frontend.Core.Schemas; using Iceshrimp.Frontend.Core.Schemas;
using Iceshrimp.Shared.Schemas;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
namespace Iceshrimp.Frontend.Core.Services; namespace Iceshrimp.Frontend.Core.Services;
@ -50,7 +49,7 @@ internal class SessionService
WriteUsers(); WriteUsers();
} }
private StoredUser? GetUserById(string id) private StoredUser GetUserById(string id)
{ {
var user = Users[id]; var user = Users[id];
return user; return user;

View file

@ -3,15 +3,15 @@
@using Iceshrimp.Frontend.Core.Schemas @using Iceshrimp.Frontend.Core.Schemas
@using Iceshrimp.Frontend.Core.Services @using Iceshrimp.Frontend.Core.Services
@using Iceshrimp.Shared.Schemas @using Iceshrimp.Shared.Schemas
@inject ApiService Api @inject ApiService Api
@inject SessionService SessionService @inject SessionService SessionService
@inject NavigationManager Navigation @inject NavigationManager Navigation
<h3>Login</h3> <h3>Login</h3>
<div> <div>
<input <input
@bind="@Username" /> @bind="@Username"/>
<input <input
@bind="@Password" /> @bind="@Password"/>
<button @onclick="Submit" disabled="@Loading">Login</button> <button @onclick="Submit" disabled="@Loading">Login</button>
</div> </div>
@if (Loading) @if (Loading)
@ -36,13 +36,21 @@
Loading = true; Loading = true;
try try
{ {
if (Username == null || Password == null)
{
Loading = false;
Failure = true;
StateHasChanged(); // Manually triggering a state update, else component will not re-render.
return;
}
var res = await Api.Auth.Login(new AuthRequest { Username = Username, Password = Password }); var res = await Api.Auth.Login(new AuthRequest { Username = Username, Password = Password });
switch (res.Status) switch (res.Status)
{ {
case AuthStatusEnum.Authenticated: case AuthStatusEnum.Authenticated:
SessionService.AddUser(new StoredUser SessionService.AddUser(new StoredUser
{ // Token nor user will ever be null on an authenticated response { // Token nor user will ever be null on an authenticated response
Id = res.User!.Id, Id = res.User!.Id,
Username = res.User.Username, Username = res.User.Username,
DisplayName = res.User.DisplayName, DisplayName = res.User.DisplayName,
AvatarUrl = res.User.AvatarUrl, AvatarUrl = res.User.AvatarUrl,
@ -70,4 +78,5 @@
StateHasChanged(); // Manually triggering a state update, else component will not re-render. StateHasChanged(); // Manually triggering a state update, else component will not re-render.
} }
} }
} }

View file

@ -3,31 +3,31 @@
@using Iceshrimp.Frontend.Core.Services @using Iceshrimp.Frontend.Core.Services
@using Microsoft.AspNetCore.Authorization @using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Components.Authorization @using Microsoft.AspNetCore.Components.Authorization
@inject SessionService Session @inject SessionService Session
@inject NavigationManager Navigation @inject NavigationManager Navigation
<h3>Timeline</h3> <h3>Timeline</h3>
@if (username != null) @if (_username != null)
{ {
<span>You are logged in as @username </span> <span>You are logged in as @_username </span>
<AuthorizeView> <AuthorizeView>
<p>Authorization says you are @context.User.Identity!.Name</p> <p>Authorization says you are @context.User.Identity!.Name</p>
</AuthorizeView> </AuthorizeView>
<button @onclick="Logout">Logout</button> <button @onclick="Logout">Logout</button>
}else }
else
{ {
<span>Not logged in</span> <span>Not logged in</span>
} }
@code { @code {
private string? username; private string? _username;
protected override void OnInitialized() protected override void OnInitialized()
{ {
if (Session.Current != null) if (Session.Current != null)
{ {
username = Session.Current.Username; _username = Session.Current.Username;
} }
} }
@ -36,4 +36,5 @@
Session.EndSession(); Session.EndSession();
Navigation.NavigateTo("/login"); Navigation.NavigateTo("/login");
} }
} }