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