feat: prolong token UI logic implemented

This commit is contained in:
Mikhail Shabanov committed 2021-09-01 17:35:40 +02:00
1 parent 6390c454d0
commit 7d1d1b5ad6
9 files changed
+223 -13

No files matched your search

@@ -1,23 +1,56 @@
using System.Threading;
using System;
using System.Threading;
using System.Threading.Tasks;
using MediatR;
using Microsoft.AspNetCore.Components;
using WotBlitzStatisticsPro.Blazor.Messages;
using WotBlitzStatisticsPro.Blazor.Model;
namespace WotBlitzStatisticsPro.Blazor.Services
{
public class NavigationMessagesInterceptor: INotificationHandler<OpenPlayerInfoMessage>
{
private readonly NavigationManager _navigationManager;
private readonly IMediator _mediator;
private readonly ILocalStorageService _localStorageService;
public NavigationMessagesInterceptor(NavigationManager navigationManager)
public NavigationMessagesInterceptor(
NavigationManager navigationManager,
ILocalStorageService localStorageService,
IMediator mediator)
{
_navigationManager = navigationManager;
_localStorageService = localStorageService;
_mediator = mediator;
}
public Task Handle(OpenPlayerInfoMessage notification, CancellationToken cancellationToken)
public async Task Handle(OpenPlayerInfoMessage notification, CancellationToken cancellationToken)
{
if(notification.IsLoggedIn)
{
await CheckExpirationDateAndProlongIfNeeded();
}
_navigationManager.NavigateTo($"/player/{notification.AccountId}");
return Task.CompletedTask;
}
private async Task CheckExpirationDateAndProlongIfNeeded()
{
int daysBeforeProlong = 5;
var loggedInData = await _localStorageService.GetItemAsync<LoginInfo>(Constants.LoginInfoLocalStorageKey);
if(loggedInData != null)
{
var tokenExpiration = (new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc))
.AddSeconds(loggedInData.ExpiresAt).ToLocalTime();
var expirationDaysDifference = (tokenExpiration - DateTime.Now).TotalDays;
if(expirationDaysDifference <= 0)
{
await _mediator.Publish(new RedirectToWgLoginMessage(loggedInData.Realm));
}
if(expirationDaysDifference > 0 && expirationDaysDifference <= daysBeforeProlong)
{
await _mediator.Publish(new ProlongWargamingAccessToken());
}
}
}
}
}