Updated front-end authentication.
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
@using JSMR.UI.Blazor.Services
|
||||
@using Microsoft.AspNetCore.Components.Routing
|
||||
|
||||
@inject SessionState Session
|
||||
@inject NavigationManager Navigation
|
||||
|
||||
@if (!_ready)
|
||||
{
|
||||
<p>Loading...</p>
|
||||
}
|
||||
else
|
||||
{
|
||||
@ChildContent
|
||||
}
|
||||
|
||||
@code {
|
||||
[Parameter] public RenderFragment? ChildContent { get; set; }
|
||||
|
||||
private bool _ready;
|
||||
|
||||
// Add any routes you want public here.
|
||||
// Use absolute-path form (leading slash).
|
||||
private static readonly HashSet<string> _allowAnonymous = new(StringComparer.OrdinalIgnoreCase)
|
||||
{
|
||||
"/login",
|
||||
"/login/", // optional
|
||||
};
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
Navigation.LocationChanged += OnLocationChanged;
|
||||
|
||||
// One-time refresh at app start
|
||||
await Session.RefreshAsync();
|
||||
_ready = true;
|
||||
|
||||
await EnforceAsync();
|
||||
}
|
||||
|
||||
private async void OnLocationChanged(object? sender, LocationChangedEventArgs e)
|
||||
{
|
||||
// If your Session can change based on navigation/cookies, you *may* refresh here,
|
||||
// but avoid doing it on every navigation unless necessary.
|
||||
// await Session.RefreshAsync();
|
||||
|
||||
await EnforceAsync();
|
||||
}
|
||||
|
||||
private Task EnforceAsync()
|
||||
{
|
||||
if (!_ready) return Task.CompletedTask;
|
||||
|
||||
var path = "/" + Navigation.ToBaseRelativePath(Navigation.Uri);
|
||||
var qIndex = path.IndexOf('?', StringComparison.Ordinal);
|
||||
if (qIndex >= 0) path = path[..qIndex];
|
||||
|
||||
// allow anonymous routes
|
||||
if (_allowAnonymous.Contains(path))
|
||||
return Task.CompletedTask;
|
||||
|
||||
if (!Session.IsAuthenticated)
|
||||
{
|
||||
var returnUrl = Uri.EscapeDataString(Navigation.Uri);
|
||||
Navigation.NavigateTo($"/login?returnUrl={returnUrl}", forceLoad: false);
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
=> Navigation.LocationChanged -= OnLocationChanged;
|
||||
}
|
||||
@@ -5,7 +5,11 @@
|
||||
<div class="@GetClasses()" @onclick="@OnClickAsync">
|
||||
@if (Graphic != null)
|
||||
{
|
||||
<Icon Graphic="@Graphic.Value" Varient="@(IconVarient ?? Enums.IconVarient.None)" Color="@Color"></Icon>
|
||||
<Icon Graphic="@Graphic.Value"
|
||||
Varient="@(IconVarient ?? Enums.IconVarient.None)"
|
||||
Size="@(IconSize ?? Enums.SizeVarient.Small)"
|
||||
Color="@Color">
|
||||
</Icon>
|
||||
}
|
||||
<span>@ChildContent</span>
|
||||
</div>
|
||||
@@ -15,7 +19,12 @@ else
|
||||
<a class="@GetClasses()" href="@Url" target="@Target">
|
||||
@if (Graphic != null)
|
||||
{
|
||||
<Icon Graphic="@Graphic.Value" Varient="@(IconVarient ?? Enums.IconVarient.None)" Color="@Color"></Icon>
|
||||
<Icon
|
||||
Graphic="@Graphic.Value"
|
||||
Varient="@(IconVarient ?? Enums.IconVarient.None)"
|
||||
Size="@(IconSize ?? Enums.SizeVarient.Small)"
|
||||
Color="@Color">
|
||||
</Icon>
|
||||
}
|
||||
<span>@ChildContent</span>
|
||||
</a>
|
||||
@@ -32,6 +41,9 @@ else
|
||||
[Parameter]
|
||||
public IconVarient? IconVarient { get; set; }
|
||||
|
||||
[Parameter]
|
||||
public SizeVarient? IconSize { get; set; }
|
||||
|
||||
[Parameter]
|
||||
public ColorVarient Color { get; set; } = ColorVarient.Primary;
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
public Graphic Graphic { get; set; }
|
||||
|
||||
[Parameter]
|
||||
public SizeVarient Size { get; set; } = SizeVarient.Medium;
|
||||
public SizeVarient Size { get; set; } = SizeVarient.Small;
|
||||
|
||||
[Parameter]
|
||||
public IconVarient Varient { get; set; } = IconVarient.None;
|
||||
@@ -25,7 +25,7 @@
|
||||
[
|
||||
$"j-icon",
|
||||
$"j-icon-{graphic}",
|
||||
$"j-icon-size-{Size.ToString().ToLower()}",
|
||||
$"size-{Size.ToString().ToLower()}",
|
||||
$"background-color-{Color.ToString().ToLower()}"
|
||||
];
|
||||
|
||||
|
||||
@@ -42,12 +42,12 @@
|
||||
<ProductTag Tag="tag"></ProductTag>
|
||||
}
|
||||
</div>
|
||||
<div class="j-tags">
|
||||
@* <div class="j-tags">
|
||||
@foreach (var tag in Product.Tags)
|
||||
{
|
||||
@* <TagChip Tag="tag"></TagChip> *@
|
||||
<TagChip Tag="tag"></TagChip>
|
||||
}
|
||||
</div>
|
||||
</div> *@
|
||||
</div>
|
||||
<div class="j-voice-work-info">
|
||||
<div class="j-release-date-container">
|
||||
|
||||
Reference in New Issue
Block a user