feat: saving logged-in user to local storage + tests

This commit is contained in:
Mikhail Shabanov committed 2021-08-27 18:15:11 +02:00
1 parent 07cda06fea
commit 6cbb809f44
8 files changed
+264 -13

No files matched your search

@@ -39,5 +39,7 @@
public const string CountryOther = "other";
public const string CountryEuropean = "european";
public const string LoginInfoLocalStorageKey = "login_info";
}
}
@@ -0,0 +1,13 @@
using WotBlitzStatisticsPro.Blazor.GraphQl;
namespace WotBlitzStatisticsPro.Blazor.Model
{
public class LoginInfo
{
public RealmType Realm { get; set; }
public string NickName { get; set; }
public long AccountId { get; set; }
public string AccessToken { get; set; }
public long ExpiresAt { get; set; }
}
}
@@ -15,7 +15,7 @@
<RadzenLabel Text=@Localize["Region"] />
</div>
<div class="col-md-4">
<RadzenDropDown @bind-Value=@_currentRealmType
<RadzenDropDown @bind-Value=@CurrentRealmType
Data=@_realms
ValueProperty="RealmType"
Style="margin-bottom: 10px; width: 87px"
@@ -34,7 +34,7 @@
</div>
@code {
RealmType _currentRealmType = RealmType.Eu;
public RealmType CurrentRealmType { get; set; } = RealmType.Eu;
readonly List<RealmSelector> _realms = new()
{
@@ -47,7 +47,7 @@
protected override async Task OnInitializedAsync()
{
var settings = await LocalStorageService.ReadSettings();
_currentRealmType = settings.RealmType;
CurrentRealmType = settings.RealmType;
}
public async Task OnRealmChanged(object value)
@@ -60,7 +60,7 @@
public async Task RedirectToWg()
{
await Mediator.Publish(new RedirectToWgLoginMessage(_currentRealmType));
await Mediator.Publish(new RedirectToWgLoginMessage(CurrentRealmType));
DialogService.Close(true);
}
@@ -1,11 +1,11 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading;
using System.Threading.Tasks;
using MediatR;
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.Localization;
using Radzen;
using WotBlitzStatisticsPro.Blazor.Messages;
using WotBlitzStatisticsPro.Blazor.Model;
using WotBlitzStatisticsPro.Blazor.Pages;
namespace WotBlitzStatisticsPro.Blazor.Services
@@ -19,17 +19,23 @@ namespace WotBlitzStatisticsPro.Blazor.Services
private readonly DialogService _dialogService;
private readonly IStringLocalizer<App> _localizer;
private readonly NavigationManager _navigationManager;
private readonly ILocalStorageService _localStorage;
private readonly IMediator _mediator;
public WargamingLoginService(
IGraphQlBackendService graphQlBackendService,
DialogService dialogService,
IStringLocalizer<App> localizer,
NavigationManager navigationManager)
NavigationManager navigationManager,
ILocalStorageService localStorage,
IMediator mediator)
{
_graphQlBackendService = graphQlBackendService;
_dialogService = dialogService;
_localizer = localizer;
_navigationManager = navigationManager;
_localStorage = localStorage;
_mediator = mediator;
}
public async Task Handle(LoginToWgMessage notification, CancellationToken cancellationToken)
@@ -49,11 +55,18 @@ namespace WotBlitzStatisticsPro.Blazor.Services
_navigationManager.NavigateTo(url);
}
public Task Handle(RedirectFromWgLoginMessage notification, CancellationToken cancellationToken)
public async Task Handle(RedirectFromWgLoginMessage notification, CancellationToken cancellationToken)
{
// ToDo: Store login info to local storage
// ToDo: Send OpenPlayerInfoMessage with isLoggedIn = true
return Task.CompletedTask;
// Store login info to local storage
await _localStorage.SetItemAsync(Constants.LoginInfoLocalStorageKey, new LoginInfo {
Realm = notification.Realm,
AccessToken = notification.AccessToken,
AccountId = notification.AccountId,
ExpiresAt = notification.ExpiresAt,
NickName = notification.NickName, });
// Send OpenPlayerInfoMessage with isLoggedIn = true
await _mediator.Publish(new OpenPlayerInfoMessage(notification.AccountId, true));
}
}
}