This commit is contained in:
femsci 2023-10-01 11:08:25 +02:00
parent 51e29884cd
commit ac4c7c0656
Signed by: femsci
GPG key ID: 08F7911F0E650C67
8 changed files with 59 additions and 12 deletions

View file

@ -1,5 +1,6 @@
<Router AppAssembly="@typeof(App).Assembly">
<Found Context="routeData">
<AuthenticateUser />
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
<FocusOnNavigate RouteData="@routeData" Selector="h1" />
</Found>

View file

@ -40,7 +40,15 @@
public LoginModel Model { get; set; } = new();
public string ValidationMsg = string.Empty;
protected override void OnInitialized() => Model ??= new();
protected async override Task OnInitializedAsync()
{
if (await usr.GetUser() != null)
{
nav.NavigateTo("/");
return;
}
Model ??= new();
}
private async Task Submit()
{
@ -56,7 +64,7 @@
this.StateHasChanged();
return;
}
usr.User = user;
usr.SetUser(user);
this.StateHasChanged();
nav.NavigateTo("/");
return;

View file

@ -10,7 +10,7 @@
var resp = await req.GetAsync("/api/auth/logout");
if (resp.StatusCode == HttpStatusCode.NoContent)
{
usr.User = null!;
usr.SetUser(null!);
}
nav.NavigateTo("/login");
}

View file

@ -52,7 +52,15 @@
public LoginModel Model { get; set; } = new();
public string ValidationMsg = string.Empty;
protected override void OnInitialized() => Model ??= new();
protected override async Task OnInitializedAsync()
{
if (await usr.GetUser() != null)
{
nav.NavigateTo("/");
return;
}
Model ??= new();
}
private async Task Submit()
{
@ -67,7 +75,7 @@
this.StateHasChanged();
return;
}
usr.User = user;
usr.SetUser(user);
this.StateHasChanged();
nav.NavigateTo("/");
return;

View file

@ -27,8 +27,6 @@ internal class Program
var app = builder.Build();
await app.Services.GetRequiredService<UserManager>().InitAsync();
await builder.Build().RunAsync();
}
}

View file

@ -16,6 +16,8 @@ public class UserManager
}
private readonly IServiceProvider _serv;
private bool _init = false;
static SemaphoreSlim sem = new SemaphoreSlim(1, 1);
public async Task InitAsync()
@ -24,11 +26,30 @@ public class UserManager
var resp = await scope.ServiceProvider.GetRequiredService<HttpClient>().GetAsync("/api/auth/userdata");
if (resp.StatusCode == HttpStatusCode.OK)
{
User = (await resp.Content.ReadFromJsonAsync<UserModel>())!;
this.User = (await resp.Content.ReadFromJsonAsync<UserModel>())!;
Console.WriteLine($"User: {User.Id}");
}
}
public UserModel User { get; set; } = default!;
private UserModel User { get; set; } = default!;
public async Task<UserModel?> GetUser()
{
if (User == null)
{
await sem.WaitAsync();
if (!_init)
{
await InitAsync();
}
sem.Release();
}
return User;
}
public void SetUser(UserModel u)
{
User = u;
}
public bool IsAuthorized => User != null;
}

View file

@ -0,0 +1,10 @@
@inject UserManager usr
@code {
protected override async Task OnInitializedAsync()
{
await Task.Yield();
await usr.InitAsync();
this.StateHasChanged();
}
}

View file

@ -15,7 +15,7 @@
}
else
{
<a href="/logout">Log out (@usr.User.Username)</a>
<a href="/logout">Log out (@u?.Username)</a>
}
</div>
@ -26,9 +26,10 @@
</div>
@code {
private UserModel u;
protected override async Task OnInitializedAsync()
{
Console.WriteLine($"meow: {usr.IsAuthorized}");
Console.WriteLine($"Meow: {usr.User.Id}");
await Task.Yield();
u = await usr.GetUser()!;
}
}