@using JSMR.UI.Blazor.Services

@inject SessionState Session
@inject NavigationManager Nav

@if (!ready)
{
    <p>Loading...</p>
}
else if (!Session.IsAuthenticated)
{
    <!-- nothing shown, we redirect -->
}
else
{
    @ChildContent
}

@code {
    [Parameter] public RenderFragment? ChildContent { get; set; }

    private bool ready;

    protected override async Task OnInitializedAsync()
    {
        await Session.RefreshAsync();
        ready = true;

        if (!Session.IsAuthenticated)
        {
            var returnUrl = Uri.EscapeDataString(Nav.Uri);
            Nav.NavigateTo($"/login?returnUrl={returnUrl}", forceLoad: false);
        }
    }
}