Added automatic token refresh feature and login page
This commit is contained in:
@@ -2,12 +2,13 @@
|
||||
@using HopFrame.Security.Claims
|
||||
@using Microsoft.AspNetCore.Http
|
||||
|
||||
@if (IsAuthorized()) {
|
||||
@if (HandleComponent()) {
|
||||
@ChildContent
|
||||
}
|
||||
|
||||
@inject ITokenContext Auth
|
||||
@inject IHttpContextAccessor HttpAccessor
|
||||
@inject NavigationManager Navigator
|
||||
|
||||
@code {
|
||||
[Parameter]
|
||||
@@ -16,14 +17,17 @@
|
||||
[Parameter]
|
||||
public string Permission { get; set; }
|
||||
|
||||
[Parameter]
|
||||
public string RedirectIfUnauthorized { get; set; }
|
||||
|
||||
[Parameter]
|
||||
public RenderFragment ChildContent { get; set; }
|
||||
|
||||
private bool IsAuthorized() {
|
||||
if (!Auth.IsAuthenticated) return false;
|
||||
if (Permissions.Length == 0 && string.IsNullOrEmpty(Permission)) return true;
|
||||
if ((Permissions == null || Permissions.Length == 0) && string.IsNullOrEmpty(Permission)) return true;
|
||||
|
||||
var perms = new List<string>(Permissions);
|
||||
var perms = new List<string>(Permissions!);
|
||||
if (!string.IsNullOrEmpty(Permission)) perms.Add(Permission);
|
||||
|
||||
var permissions = HttpAccessor.HttpContext?.User.GetPermissions();
|
||||
@@ -31,4 +35,14 @@
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool HandleComponent() {
|
||||
var authorized = IsAuthorized();
|
||||
|
||||
if (authorized == false && !string.IsNullOrEmpty(RedirectIfUnauthorized)) {
|
||||
Navigator.NavigateTo(RedirectIfUnauthorized, true);
|
||||
}
|
||||
|
||||
return authorized;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user