@inject NavigationManager Nav

@code {
    protected override void OnAfterRender(bool firstRender)
    {
        if (!firstRender) return;

        var relative = Nav.ToBaseRelativePath(Nav.Uri);

        if (!string.IsNullOrEmpty(relative) &&
            relative.StartsWith("login", StringComparison.OrdinalIgnoreCase))
        {
            return;
        }

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