autoredirect to login page

This commit is contained in:
Ivy Collective 2024-12-23 18:41:17 -05:00
parent ea7066ae93
commit 4d2cf4bbc5

View file

@ -3,20 +3,8 @@
@inject APIService api
@inject NavigationManager Navigation
@if (IsReady) {
@if (IsAuthenticated) {
@ChildContent
} else {
<FluentLayout>
<FluentHeader>
Blazing Console
</FluentHeader>
<FluentBodyContent>
<h1>Log in first</h1>
<FluentButton Appearance="@Appearance.Accent" OnClick="LoginButtonClicked">Log in</FluentButton>
</FluentBodyContent>
</FluentLayout>
}
@if (IsAuthenticated) {
@ChildContent
} else {
@if (LoadingContent != null) {
@LoadingContent
@ -25,29 +13,24 @@
}
}
@code {
public bool IsReady { get; set; } = false;
public bool IsAuthenticated { get; set; } = false;
protected override async Task OnInitializedAsync()
{
string? jwt = await localStorage.GetItemAsync<string>("jwt");
if (jwt == null) {
IsAuthenticated = false;
IsReady = true;
Navigation.NavigateTo("/login", false);
return;
}
api.SetJwt(jwt);
bool authorized = await api.TestAuthorization();
IsAuthenticated = authorized;
IsReady = true;
}
public void LoginButtonClicked() {
Navigation.NavigateTo("/login", false);
if (authorized) {
IsAuthenticated = true;
} else {
Navigation.NavigateTo("/login", false);
}
}
[Parameter]