authentication fix

This commit is contained in:
2024-09-17 20:32:22 +02:00
Unverified
parent 7976e1936a
commit 1498e3bd6c
15 changed files with 149 additions and 76 deletions

View File

@@ -1,3 +1,4 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using WatchIt.Database;
@@ -5,13 +6,36 @@ using WatchIt.Database.Model.Account;
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)
{
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 actionTask = Action();
@@ -22,9 +46,16 @@ public class DeleteExpiredRefreshTokensService(ILogger<DeleteExpiredRefreshToken
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);
database.AccountRefreshTokens.AttachRange(tokens);
database.AccountRefreshTokens.RemoveRange(tokens);
await database.SaveChangesAsync();
}
}
#endregion
}

View File

@@ -121,7 +121,7 @@ public static class Program
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;
}
@@ -134,15 +134,15 @@ public static class Program
private static WebApplicationBuilder SetupServices(this WebApplicationBuilder builder)
{
// Utility
builder.Services.AddSingleton<IConfigurationService, ConfigurationService>();
builder.Services.AddSingleton<ITokensService, TokensService>();
builder.Services.AddSingleton<IUserService, UserService>();
builder.Services.AddTransient<IConfigurationService, ConfigurationService>();
builder.Services.AddTransient<ITokensService, TokensService>();
builder.Services.AddTransient<IUserService, UserService>();
// Controller
builder.Services.AddSingleton<IAccountsControllerService, AccountsControllerService>();
builder.Services.AddSingleton<IGenresControllerService, GenresControllerService>();
builder.Services.AddSingleton<IMoviesControllerService, MoviesControllerService>();
builder.Services.AddSingleton<IMediaControllerService, MediaControllerService>();
builder.Services.AddTransient<IAccountsControllerService, AccountsControllerService>();
builder.Services.AddTransient<IGenresControllerService, GenresControllerService>();
builder.Services.AddTransient<IMoviesControllerService, MoviesControllerService>();
builder.Services.AddTransient<IMediaControllerService, MediaControllerService>();
return builder;
}

View File

@@ -114,7 +114,12 @@ public class JWTAuthenticationStateProvider : AuthenticationStateProvider
{
AuthenticateResponse? response = null;
await _accountsService.AuthenticateRefresh((data) => response = data);
void SetResponse(AuthenticateResponse data)
{
response = data;
}
await _accountsService.AuthenticateRefresh(SetResponse);
if (response is not null)
{
@@ -151,8 +156,9 @@ public class JWTAuthenticationStateProvider : AuthenticationStateProvider
public static DateTime ConvertFromUnixTimestamp(int timestamp)
{
DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
return origin.AddSeconds(timestamp);
DateTime date = new DateTime(1970, 1, 1, 0, 0, 0, 0);
date = date.AddSeconds(timestamp);
return date;
}
#endregion

View File

@@ -9,8 +9,8 @@
<link rel="icon" type="image/png" href="favicon.png"/>
<!-- CSS -->
<link rel="stylesheet" href="app.css?version=0.7"/>
<link rel="stylesheet" href="WatchIt.Website.styles.css?version=0.7"/>
<link rel="stylesheet" href="app.css?version=0.13"/>
<link rel="stylesheet" href="WatchIt.Website.styles.css?version=0.12"/>
<!-- BOOTSTRAP -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>

View File

@@ -4,21 +4,35 @@
<br/>
<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)
{
<div id="posterButtons" class="container-fluid mt-2">
<div class="row">
<button type="button" class="col btn btn-secondary me-1" @onclick="SavePoster" disabled=@(!Id.HasValue) autocomplete="off">Save poster</button>
<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-secondary btn-block btn-stretch-x" @onclick="SavePoster" disabled=@(!Id.HasValue || _posterLoading) autocomplete="off">Save poster</button>
</div>
<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>
}
else
{
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>
<div class="col rounded-3 panel panel-regular m-1 p-3">
@@ -73,6 +87,8 @@
</div>
</div>
<style>
#posterInput, #posterButtons {
width: 300px;

View File

@@ -37,6 +37,7 @@ public partial class MediaFormComponent : ComponentBase
private bool _posterChanged = false;
private string? _posterBase64 = null;
private string? _posterMediaType = null;
private bool _posterLoading = false;
#endregion
@@ -84,6 +85,7 @@ public partial class MediaFormComponent : ComponentBase
_actualPosterBase64 = _posterBase64;
_actualPosterMediaType = _posterMediaType;
_posterChanged = false;
_posterLoading = false;
}
MediaPosterRequest data = new MediaPosterRequest
@@ -92,6 +94,7 @@ public partial class MediaFormComponent : ComponentBase
MimeType = _posterMediaType
};
_posterLoading = true;
await MediaWebAPIService.PutPoster(Id.Value, data, SuccessAction);
}
@@ -104,8 +107,10 @@ public partial class MediaFormComponent : ComponentBase
_posterChanged = false;
_posterBase64 = null;
_posterMediaType = null;
_posterLoading = false;
}
_posterLoading = true;
await MediaWebAPIService.DeletePoster(Id.Value, SuccessAction);
}

View File

@@ -1,3 +1,5 @@
#posterInput {
/* IDS */
#posterInput, #posterButtons {
width: 300px;
}

View File

@@ -31,26 +31,3 @@
}
</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();
}
}
}

View File

@@ -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
}

View File

@@ -101,15 +101,10 @@
<style>
html {
height: 100%;
}
body {
background-image: url('@_background');
height: 100%;
background-image: url('@_background');
background-position: center;
background-repeat: no-repeat;
background-size: cover;

View File

@@ -1,7 +1,13 @@
body {
background-size: cover;
/* TAGS */
html {
height: 100%;
}
/* CLASSES */
.logo {
font-size: 60px;
margin: 10px;

View File

@@ -1,6 +1,6 @@
namespace WatchIt.Website.Pages;
public class HomePage_razor
public partial class HomePage
{
}

View File

@@ -1,6 +1 @@
@page "/movies/{id:long}"
@code {
}

View File

@@ -13,7 +13,7 @@
<h2>@(Id is null ? "Create new movie" : $"Edit movie \"{_movieInfo.Title}\"")</h2>
</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
{

View File

@@ -81,3 +81,7 @@ body, html {
right: auto;
left: 0;
}
.btn-stretch-x {
width: 100%;
}