authentication fix
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using WatchIt.Database;
|
using WatchIt.Database;
|
||||||
@@ -5,13 +6,36 @@ using WatchIt.Database.Model.Account;
|
|||||||
|
|
||||||
namespace WatchIt.WebAPI.WorkerServices;
|
namespace WatchIt.WebAPI.WorkerServices;
|
||||||
|
|
||||||
public class DeleteExpiredRefreshTokensService(ILogger<DeleteExpiredRefreshTokensService> logger, DatabaseContext database) : BackgroundService
|
public class DeleteExpiredRefreshTokensService : BackgroundService
|
||||||
{
|
{
|
||||||
|
#region SERVICES
|
||||||
|
|
||||||
|
private readonly ILogger<DeleteExpiredRefreshTokensService> _logger;
|
||||||
|
private readonly IServiceScopeFactory _serviceScopeFactory;
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#region CONSTRUCTORS
|
||||||
|
|
||||||
|
public DeleteExpiredRefreshTokensService(ILogger<DeleteExpiredRefreshTokensService> logger, IServiceScopeFactory serviceScopeFactory)
|
||||||
|
{
|
||||||
|
_logger = logger;
|
||||||
|
_serviceScopeFactory = serviceScopeFactory;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#region PUBLIC METHODS
|
||||||
|
|
||||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||||
{
|
{
|
||||||
while (!stoppingToken.IsCancellationRequested)
|
while (!stoppingToken.IsCancellationRequested)
|
||||||
{
|
{
|
||||||
logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
|
_logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
|
||||||
|
|
||||||
Task delayTask = Task.Delay(300000, stoppingToken);
|
Task delayTask = Task.Delay(300000, stoppingToken);
|
||||||
Task actionTask = Action();
|
Task actionTask = Action();
|
||||||
@@ -22,9 +46,16 @@ public class DeleteExpiredRefreshTokensService(ILogger<DeleteExpiredRefreshToken
|
|||||||
|
|
||||||
protected async Task Action()
|
protected async Task Action()
|
||||||
{
|
{
|
||||||
|
using (IServiceScope scope = _serviceScopeFactory.CreateScope())
|
||||||
|
{
|
||||||
|
DatabaseContext database = scope.ServiceProvider.GetService<DatabaseContext>();
|
||||||
|
|
||||||
IEnumerable<AccountRefreshToken> tokens = database.AccountRefreshTokens.Where(x => x.ExpirationDate < DateTime.UtcNow);
|
IEnumerable<AccountRefreshToken> tokens = database.AccountRefreshTokens.Where(x => x.ExpirationDate < DateTime.UtcNow);
|
||||||
database.AccountRefreshTokens.AttachRange(tokens);
|
database.AccountRefreshTokens.AttachRange(tokens);
|
||||||
database.AccountRefreshTokens.RemoveRange(tokens);
|
database.AccountRefreshTokens.RemoveRange(tokens);
|
||||||
await database.SaveChangesAsync();
|
await database.SaveChangesAsync();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
@@ -121,7 +121,7 @@ public static class Program
|
|||||||
|
|
||||||
private static WebApplicationBuilder SetupDatabase(this WebApplicationBuilder builder)
|
private static WebApplicationBuilder SetupDatabase(this WebApplicationBuilder builder)
|
||||||
{
|
{
|
||||||
builder.Services.AddDbContext<DatabaseContext>(x => x.UseLazyLoadingProxies().UseNpgsql(builder.Configuration.GetConnectionString("Default")), ServiceLifetime.Singleton);
|
builder.Services.AddDbContext<DatabaseContext>(x => x.UseLazyLoadingProxies().UseNpgsql(builder.Configuration.GetConnectionString("Default")), ServiceLifetime.Transient);
|
||||||
return builder;
|
return builder;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -134,15 +134,15 @@ public static class Program
|
|||||||
private static WebApplicationBuilder SetupServices(this WebApplicationBuilder builder)
|
private static WebApplicationBuilder SetupServices(this WebApplicationBuilder builder)
|
||||||
{
|
{
|
||||||
// Utility
|
// Utility
|
||||||
builder.Services.AddSingleton<IConfigurationService, ConfigurationService>();
|
builder.Services.AddTransient<IConfigurationService, ConfigurationService>();
|
||||||
builder.Services.AddSingleton<ITokensService, TokensService>();
|
builder.Services.AddTransient<ITokensService, TokensService>();
|
||||||
builder.Services.AddSingleton<IUserService, UserService>();
|
builder.Services.AddTransient<IUserService, UserService>();
|
||||||
|
|
||||||
// Controller
|
// Controller
|
||||||
builder.Services.AddSingleton<IAccountsControllerService, AccountsControllerService>();
|
builder.Services.AddTransient<IAccountsControllerService, AccountsControllerService>();
|
||||||
builder.Services.AddSingleton<IGenresControllerService, GenresControllerService>();
|
builder.Services.AddTransient<IGenresControllerService, GenresControllerService>();
|
||||||
builder.Services.AddSingleton<IMoviesControllerService, MoviesControllerService>();
|
builder.Services.AddTransient<IMoviesControllerService, MoviesControllerService>();
|
||||||
builder.Services.AddSingleton<IMediaControllerService, MediaControllerService>();
|
builder.Services.AddTransient<IMediaControllerService, MediaControllerService>();
|
||||||
|
|
||||||
return builder;
|
return builder;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -114,7 +114,12 @@ public class JWTAuthenticationStateProvider : AuthenticationStateProvider
|
|||||||
{
|
{
|
||||||
AuthenticateResponse? response = null;
|
AuthenticateResponse? response = null;
|
||||||
|
|
||||||
await _accountsService.AuthenticateRefresh((data) => response = data);
|
void SetResponse(AuthenticateResponse data)
|
||||||
|
{
|
||||||
|
response = data;
|
||||||
|
}
|
||||||
|
|
||||||
|
await _accountsService.AuthenticateRefresh(SetResponse);
|
||||||
|
|
||||||
if (response is not null)
|
if (response is not null)
|
||||||
{
|
{
|
||||||
@@ -151,8 +156,9 @@ public class JWTAuthenticationStateProvider : AuthenticationStateProvider
|
|||||||
|
|
||||||
public static DateTime ConvertFromUnixTimestamp(int timestamp)
|
public static DateTime ConvertFromUnixTimestamp(int timestamp)
|
||||||
{
|
{
|
||||||
DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
|
DateTime date = new DateTime(1970, 1, 1, 0, 0, 0, 0);
|
||||||
return origin.AddSeconds(timestamp);
|
date = date.AddSeconds(timestamp);
|
||||||
|
return date;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|||||||
@@ -9,8 +9,8 @@
|
|||||||
<link rel="icon" type="image/png" href="favicon.png"/>
|
<link rel="icon" type="image/png" href="favicon.png"/>
|
||||||
|
|
||||||
<!-- CSS -->
|
<!-- CSS -->
|
||||||
<link rel="stylesheet" href="app.css?version=0.7"/>
|
<link rel="stylesheet" href="app.css?version=0.13"/>
|
||||||
<link rel="stylesheet" href="WatchIt.Website.styles.css?version=0.7"/>
|
<link rel="stylesheet" href="WatchIt.Website.styles.css?version=0.12"/>
|
||||||
|
|
||||||
<!-- BOOTSTRAP -->
|
<!-- BOOTSTRAP -->
|
||||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
|
||||||
|
|||||||
@@ -4,21 +4,35 @@
|
|||||||
<br/>
|
<br/>
|
||||||
<InputFile id="posterInput" class="form-control my-1" OnChange="LoadPoster" disabled=@(!Id.HasValue) autocomplete="off"/>
|
<InputFile id="posterInput" class="form-control my-1" OnChange="LoadPoster" disabled=@(!Id.HasValue) autocomplete="off"/>
|
||||||
|
|
||||||
|
@if (_posterChanged || !string.IsNullOrWhiteSpace(_actualPosterBase64))
|
||||||
|
{
|
||||||
|
<div id="posterButtons" class="container-fluid mt-2 p-0">
|
||||||
|
<div class="row gx-1">
|
||||||
@if (_posterChanged)
|
@if (_posterChanged)
|
||||||
{
|
{
|
||||||
<div id="posterButtons" class="container-fluid mt-2">
|
<div class="col">
|
||||||
<div class="row">
|
<button type="button" class="btn btn-secondary btn-block btn-stretch-x" @onclick="SavePoster" disabled=@(!Id.HasValue || _posterLoading) autocomplete="off">Save poster</button>
|
||||||
<button type="button" class="col btn btn-secondary me-1" @onclick="SavePoster" disabled=@(!Id.HasValue) autocomplete="off">Save poster</button>
|
</div>
|
||||||
<button type="button" class="col btn btn-danger ms-1" @onclick="CancelPoster" disabled=@(!Id.HasValue) autocomplete="off">Drop changes</button>
|
<div class="col">
|
||||||
|
<button type="button" class="btn btn-danger btn-block btn-stretch-x" @onclick="CancelPoster" disabled=@(!Id.HasValue || _posterLoading) autocomplete="off">Drop changes</button>
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
else if (!string.IsNullOrWhiteSpace(_actualPosterBase64))
|
||||||
|
{
|
||||||
|
<div class="col">
|
||||||
|
<button type="button" class="btn btn-danger btn-block btn-stretch-x" @onclick="DeletePoster" disabled=@(!Id.HasValue || _posterLoading) autocomplete="off">Delete poster</button>
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
@if (_posterLoading)
|
||||||
|
{
|
||||||
|
<div class="col-auto">
|
||||||
|
<div class="d-flex align-items-center justify-content-center">
|
||||||
|
<div class="spinner-border" role="status"></div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
}
|
}
|
||||||
else
|
</div>
|
||||||
{
|
</div>
|
||||||
if (!string.IsNullOrWhiteSpace(_actualPosterBase64))
|
|
||||||
{
|
|
||||||
<button id="posterButtons" type="button" class="btn btn-danger form-control mt-1" @onclick="DeletePoster" disabled=@(!Id.HasValue) autocomplete="off">Delete poster</button>
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
</div>
|
</div>
|
||||||
<div class="col rounded-3 panel panel-regular m-1 p-3">
|
<div class="col rounded-3 panel panel-regular m-1 p-3">
|
||||||
@@ -73,6 +87,8 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<style>
|
<style>
|
||||||
#posterInput, #posterButtons {
|
#posterInput, #posterButtons {
|
||||||
width: 300px;
|
width: 300px;
|
||||||
|
|||||||
@@ -37,6 +37,7 @@ public partial class MediaFormComponent : ComponentBase
|
|||||||
private bool _posterChanged = false;
|
private bool _posterChanged = false;
|
||||||
private string? _posterBase64 = null;
|
private string? _posterBase64 = null;
|
||||||
private string? _posterMediaType = null;
|
private string? _posterMediaType = null;
|
||||||
|
private bool _posterLoading = false;
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
@@ -84,6 +85,7 @@ public partial class MediaFormComponent : ComponentBase
|
|||||||
_actualPosterBase64 = _posterBase64;
|
_actualPosterBase64 = _posterBase64;
|
||||||
_actualPosterMediaType = _posterMediaType;
|
_actualPosterMediaType = _posterMediaType;
|
||||||
_posterChanged = false;
|
_posterChanged = false;
|
||||||
|
_posterLoading = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
MediaPosterRequest data = new MediaPosterRequest
|
MediaPosterRequest data = new MediaPosterRequest
|
||||||
@@ -92,6 +94,7 @@ public partial class MediaFormComponent : ComponentBase
|
|||||||
MimeType = _posterMediaType
|
MimeType = _posterMediaType
|
||||||
};
|
};
|
||||||
|
|
||||||
|
_posterLoading = true;
|
||||||
await MediaWebAPIService.PutPoster(Id.Value, data, SuccessAction);
|
await MediaWebAPIService.PutPoster(Id.Value, data, SuccessAction);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -104,8 +107,10 @@ public partial class MediaFormComponent : ComponentBase
|
|||||||
_posterChanged = false;
|
_posterChanged = false;
|
||||||
_posterBase64 = null;
|
_posterBase64 = null;
|
||||||
_posterMediaType = null;
|
_posterMediaType = null;
|
||||||
|
_posterLoading = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_posterLoading = true;
|
||||||
await MediaWebAPIService.DeletePoster(Id.Value, SuccessAction);
|
await MediaWebAPIService.DeletePoster(Id.Value, SuccessAction);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
#posterInput {
|
/* IDS */
|
||||||
|
|
||||||
|
#posterInput, #posterButtons {
|
||||||
width: 300px;
|
width: 300px;
|
||||||
}
|
}
|
||||||
@@ -31,26 +31,3 @@
|
|||||||
}
|
}
|
||||||
</div>
|
</div>
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@code {
|
|
||||||
[Inject] public IAuthenticationService AuthenticationService { get; set; } = default!;
|
|
||||||
|
|
||||||
private bool _loaded = false;
|
|
||||||
private bool _authenticated = false;
|
|
||||||
|
|
||||||
protected override async Task OnAfterRenderAsync(bool firstRender)
|
|
||||||
{
|
|
||||||
if (firstRender)
|
|
||||||
{
|
|
||||||
User? user = await AuthenticationService.GetUserAsync();
|
|
||||||
if (user is not null && user.IsAdmin)
|
|
||||||
{
|
|
||||||
_authenticated = true;
|
|
||||||
}
|
|
||||||
_loaded = true;
|
|
||||||
StateHasChanged();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,6 +1,42 @@
|
|||||||
namespace WatchIt.Website.Pages;
|
using Microsoft.AspNetCore.Components;
|
||||||
|
using WatchIt.Website.Services.Utility.Authentication;
|
||||||
|
|
||||||
public class AdminPage_razor
|
namespace WatchIt.Website.Pages;
|
||||||
|
|
||||||
|
public partial class AdminPage
|
||||||
{
|
{
|
||||||
|
#region SERVICE
|
||||||
|
|
||||||
|
[Inject] public IAuthenticationService AuthenticationService { get; set; } = default!;
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#region FIELDS
|
||||||
|
|
||||||
|
private bool _loaded = false;
|
||||||
|
private bool _authenticated = false;
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#region PRIVATE METHODS
|
||||||
|
|
||||||
|
protected override async Task OnAfterRenderAsync(bool firstRender)
|
||||||
|
{
|
||||||
|
if (firstRender)
|
||||||
|
{
|
||||||
|
User? user = await AuthenticationService.GetUserAsync();
|
||||||
|
if (user is not null && user.IsAdmin)
|
||||||
|
{
|
||||||
|
_authenticated = true;
|
||||||
|
}
|
||||||
|
_loaded = true;
|
||||||
|
StateHasChanged();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
}
|
}
|
||||||
@@ -101,15 +101,10 @@
|
|||||||
|
|
||||||
|
|
||||||
<style>
|
<style>
|
||||||
html {
|
|
||||||
height: 100%;
|
|
||||||
}
|
|
||||||
|
|
||||||
body {
|
body {
|
||||||
background-image: url('@_background');
|
|
||||||
|
|
||||||
height: 100%;
|
height: 100%;
|
||||||
|
|
||||||
|
background-image: url('@_background');
|
||||||
background-position: center;
|
background-position: center;
|
||||||
background-repeat: no-repeat;
|
background-repeat: no-repeat;
|
||||||
background-size: cover;
|
background-size: cover;
|
||||||
|
|||||||
@@ -1,7 +1,13 @@
|
|||||||
body {
|
/* TAGS */
|
||||||
background-size: cover;
|
|
||||||
|
html {
|
||||||
|
height: 100%;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* CLASSES */
|
||||||
|
|
||||||
.logo {
|
.logo {
|
||||||
font-size: 60px;
|
font-size: 60px;
|
||||||
margin: 10px;
|
margin: 10px;
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
namespace WatchIt.Website.Pages;
|
namespace WatchIt.Website.Pages;
|
||||||
|
|
||||||
public class HomePage_razor
|
public partial class HomePage
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,6 +1 @@
|
|||||||
@page "/movies/{id:long}"
|
@page "/movies/{id:long}"
|
||||||
|
|
||||||
|
|
||||||
@code {
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -13,7 +13,7 @@
|
|||||||
<h2>@(Id is null ? "Create new movie" : $"Edit movie \"{_movieInfo.Title}\"")</h2>
|
<h2>@(Id is null ? "Create new movie" : $"Edit movie \"{_movieInfo.Title}\"")</h2>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<MediaForm Data=@(_movieData) SaveDataAction=SaveData Id=@(Id) SaveDataErrors=@(_movieDataErrors) SaveDataInfo=@(_movieDataInfo)/>
|
<MediaFormComponent Data=@(_movieData) SaveDataAction=SaveData Id=@(Id) SaveDataErrors=@(_movieDataErrors) SaveDataInfo=@(_movieDataInfo)/>
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -81,3 +81,7 @@ body, html {
|
|||||||
right: auto;
|
right: auto;
|
||||||
left: 0;
|
left: 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.btn-stretch-x {
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user