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

@@ -0,0 +1,11 @@
using MediatR;
namespace WotBlitzStatisticsPro.Blazor.Messages
{
public class ProlongWargamingAccessToken: INotification
{
public ProlongWargamingAccessToken()
{
}
}
}
@@ -3,6 +3,7 @@ using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.Localization;
using WotBlitzStatisticsPro.Blazor.GraphQl;
using WotBlitzStatisticsPro.Blazor.Model;
using WotBlitzStatisticsPro.Blazor.Services;
namespace WotBlitzStatisticsPro.Blazor.Pages
@@ -30,13 +31,17 @@ namespace WotBlitzStatisticsPro.Blazor.Pages
protected override async Task OnInitializedAsync()
{
var loggedInInfo = await LocalStorageService.GetItemAsync<LoginInfo>(Constants.LoginInfoLocalStorageKey);
var settings = await LocalStorageService.ReadSettings();
CurrentRealmType = settings.RealmType;
(AccountInfo, AchievementsBySection) = await GraphQlBackendService.GetPlayerInfo(AccountId, CurrentRealmType);
await base.OnInitializedAsync();
if(loggedInInfo == null)
{
(AccountInfo, AchievementsBySection) = await GraphQlBackendService.GetPlayerInfo(AccountId, CurrentRealmType);
}
else
{
(AccountInfo, AchievementsBySection) = await GraphQlBackendService.CollectPlayerInfo(AccountId, CurrentRealmType, loggedInInfo.AccessToken);
}
}
}
}
@@ -4,6 +4,7 @@ using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using StrawberryShake;
using WotBlitzStatisticsPro.Blazor.GraphQl;
using WotBlitzStatisticsPro.Blazor.Model;
namespace WotBlitzStatisticsPro.Blazor.Services
{
@@ -99,5 +100,16 @@ namespace WotBlitzStatisticsPro.Blazor.Services
_notificationsService.ReportError("Backend error",string.Join(";", messages));
}
}
public Task<LoginInfo> ProlongToken(long accountId, string oldToken, RealmType realmType)
{
throw new System.NotImplementedException();
}
public Task<(IAccount accountInfo, IReadOnlyList<ISection> achievementsBySection)> CollectPlayerInfo(long accountId, RealmType realmType, string accessToken)
{
// ToDo: Call mutation with accessToken in the context
throw new System.NotImplementedException();
}
}
}
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using WotBlitzStatisticsPro.Blazor.GraphQl;
using WotBlitzStatisticsPro.Blazor.Model;
namespace WotBlitzStatisticsPro.Blazor.Services
{
@@ -18,6 +19,16 @@ namespace WotBlitzStatisticsPro.Blazor.Services
long accountId,
RealmType realmType);
Task<(IAccount accountInfo, IReadOnlyList<ISection> achievementsBySection)> CollectPlayerInfo(
long accountId,
RealmType realmType,
string accessToken);
Task<string> GetWgLoginUrl(RealmType realmType);
Task<LoginInfo> ProlongToken(
long accountId,
string oldToken,
RealmType realmType);
}
}
@@ -5,6 +5,7 @@ using System.Threading.Tasks;
using Bogus;
using Bogus.Extensions;
using WotBlitzStatisticsPro.Blazor.GraphQl;
using WotBlitzStatisticsPro.Blazor.Model;
namespace WotBlitzStatisticsPro.Blazor.Services.Mock
{
@@ -388,5 +389,14 @@ namespace WotBlitzStatisticsPro.Blazor.Services.Mock
return images[faker.Random.Number(images.Length - 1)];
}
public Task<LoginInfo> ProlongToken(long accountId, string oldToken, RealmType realmType)
{
return Task.FromResult(new LoginInfo { AccountId = accountId, AccessToken = oldToken, Realm = realmType, ExpiresAt = 1631182800, NickName = "Fake nick"});
}
public Task<(IAccount accountInfo, IReadOnlyList<ISection> achievementsBySection)> CollectPlayerInfo(long accountId, RealmType realmType, string accessToken)
{
return GetPlayerInfo(accountId, realmType);
}
}
}
@@ -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());
}
}
}
}
}
@@ -13,7 +13,8 @@ namespace WotBlitzStatisticsPro.Blazor.Services
public class WargamingLoginService:
INotificationHandler<LoginToWgMessage>,
INotificationHandler<RedirectToWgLoginMessage>,
INotificationHandler<RedirectFromWgLoginMessage>
INotificationHandler<RedirectFromWgLoginMessage>,
INotificationHandler<ProlongWargamingAccessToken>
{
private readonly IGraphQlBackendService _graphQlBackendService;
private readonly DialogService _dialogService;
@@ -68,5 +69,14 @@ namespace WotBlitzStatisticsPro.Blazor.Services
// Send OpenPlayerInfoMessage with isLoggedIn = true
await _mediator.Publish(new OpenPlayerInfoMessage(notification.AccountId, true));
}
public async Task Handle(ProlongWargamingAccessToken notification, CancellationToken cancellationToken)
{
var loggedInData = await _localStorage.GetItemAsync<LoginInfo>(Constants.LoginInfoLocalStorageKey);
var newLoginInfo = await _graphQlBackendService.ProlongToken(loggedInData.AccountId, loggedInData.AccessToken, loggedInData.Realm);
loggedInData.AccessToken = newLoginInfo.AccessToken;
loggedInData.ExpiresAt = newLoginInfo.ExpiresAt;
await _localStorage.SetItemAsync(Constants.LoginInfoLocalStorageKey, loggedInData);
}
}
}