blazing-console/Components/RequiresAuth.razor
Ivy Collective 2d570ea11f
All checks were successful
/ deploy (push) Successful in 2m6s
center progress ring while auth loading
2024-12-23 19:50:39 -05:00

59 lines
No EOL
1.7 KiB
Text

@*
* Copyright 2024 Ivy Collective <sys@ivycollective.dev>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*@
@using Blazored.LocalStorage
@inject ILocalStorageService localStorage
@inject APIService api
@inject NavigationManager Navigation
@if (IsAuthenticated) {
@ChildContent
} else {
@if (LoadingContent != null) {
@LoadingContent
} else {
<div style="display: flex; justify-content: center; align-items: center; min-height: 100vh;">
<FluentProgressRing Width="6rem"></FluentProgressRing>
</div>
}
}
@code {
public bool IsAuthenticated { get; set; } = false;
protected override async Task OnInitializedAsync()
{
string? jwt = await localStorage.GetItemAsync<string>("jwt");
if (jwt == null) {
Navigation.NavigateTo("/login", false);
return;
}
api.SetJwt(jwt);
bool authorized = await api.TestAuthorization();
if (authorized) {
IsAuthenticated = true;
} else {
Navigation.NavigateTo("/login", false);
}
}
[Parameter]
public required RenderFragment ChildContent { get; set; }
[Parameter]
public RenderFragment? LoadingContent { get; set; }
}