diff --git a/src/Money.UI.Blazor/Pages/Account/Login.cshtml.cs b/src/Money.UI.Blazor/Pages/Account/Login.cshtml.cs index 45959d0d..61f5daff 100644 --- a/src/Money.UI.Blazor/Pages/Account/Login.cshtml.cs +++ b/src/Money.UI.Blazor/Pages/Account/Login.cshtml.cs @@ -20,6 +20,9 @@ public class LoginBase : BlazorComponent [Inject] internal QueryString QueryString { get; set; } + [Inject] + internal TokenContainer Token { get; set; } + [Parameter] protected string ReturnUrl { get; set; } @@ -29,6 +32,12 @@ public class LoginBase : BlazorComponent protected List ErrorMessages { get; } = new List(); + protected override void OnInit() + { + if (Token.HasValue) + NavigateAway(); + } + protected override void OnParametersSet() { base.OnParametersSet(); @@ -49,13 +58,19 @@ private async Task LoginAsync(string userName, string password, bool isPermanent { if (!await ApiClient.LoginAsync(userName, password, isPermanent)) ErrorMessages.Add("User name and password don't match."); - else if (!String.IsNullOrEmpty(ReturnUrl)) - Navigator.Open(ReturnUrl); else - Navigator.OpenSummary(); + NavigateAway(); } } + private void NavigateAway() + { + if (!String.IsNullOrEmpty(ReturnUrl)) + Navigator.Open(ReturnUrl); + else + Navigator.OpenSummary(); + } + private bool Validate(string userName, string password) { if (String.IsNullOrEmpty(userName)) diff --git a/src/Money.UI.Blazor/Pages/Account/Register.cshtml.cs b/src/Money.UI.Blazor/Pages/Account/Register.cshtml.cs index 0e14988b..2bb6745d 100644 --- a/src/Money.UI.Blazor/Pages/Account/Register.cshtml.cs +++ b/src/Money.UI.Blazor/Pages/Account/Register.cshtml.cs @@ -16,11 +16,20 @@ public class RegisterBase : BlazorComponent [Inject] internal Navigator Navigator { get; set; } + [Inject] + internal TokenContainer Token { get; set; } + protected string UserName { get; set; } protected string Password { get; set; } protected string ConfirmPassword { get; set; } - public List ErrorMessages { get; } = new List(); + protected List ErrorMessages { get; } = new List(); + + protected override void OnInit() + { + if (Token.HasValue) + Navigator.OpenSummary(); + } protected async Task OnSubmitAsync() { diff --git a/src/Money.UI.Blazor/Services/ApiClient.cs b/src/Money.UI.Blazor/Services/ApiClient.cs index 9e27eeea..d25c94e3 100644 --- a/src/Money.UI.Blazor/Services/ApiClient.cs +++ b/src/Money.UI.Blazor/Services/ApiClient.cs @@ -19,8 +19,8 @@ namespace Money.Services public class ApiClient { private const string rootUrl = "http://localhost:63803"; - private static string token; + private readonly TokenContainer token; private readonly HttpClient http; private readonly CommandMapper commandMapper; private readonly QueryMapper queryMapper; @@ -28,13 +28,15 @@ public class ApiClient private readonly IEventDispatcher eventDispatcher; - public ApiClient(HttpClient http, CommandMapper commandMapper, QueryMapper queryMapper, IExceptionHandler exceptionHandler, IEventDispatcher eventDispatcher) + public ApiClient(TokenContainer token, HttpClient http, CommandMapper commandMapper, QueryMapper queryMapper, IExceptionHandler exceptionHandler, IEventDispatcher eventDispatcher) { + Ensure.NotNull(token, "token"); Ensure.NotNull(http, "http"); Ensure.NotNull(commandMapper, "commandMapper"); Ensure.NotNull(queryMapper, "queryMapper"); Ensure.NotNull(exceptionHandler, "exceptionHandler"); Ensure.NotNull(eventDispatcher, "eventDispatcher"); + this.token = token; this.http = http; this.commandMapper = commandMapper; this.queryMapper = queryMapper; @@ -47,9 +49,9 @@ public ApiClient(HttpClient http, CommandMapper commandMapper, QueryMapper query private void ClearAuthorization() { - if (token != null) + if (token.HasValue) { - token = null; + token.Value = null; http.DefaultRequestHeaders.Authorization = null; Interop.SaveToken(null); Interop.StopSignalR(); @@ -60,10 +62,10 @@ private void ClearAuthorization() private void EnsureAuthorization() { - if (token != null && http.DefaultRequestHeaders.Authorization?.Parameter != token) + if (token.HasValue && http.DefaultRequestHeaders.Authorization?.Parameter != token.Value) { - http.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); - Interop.StartSignalR(rootUrl + "/api", token); + http.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.Value); + Interop.StartSignalR(rootUrl + "/api", token.Value); eventDispatcher.PublishAsync(new UserSignedIn()); } @@ -82,11 +84,11 @@ public async Task LoginAsync(string userName, string password, bool isPerm if (!String.IsNullOrEmpty(response.Token)) { - token = response.Token; + token.Value = response.Token; EnsureAuthorization(); if (isPermanent) - Interop.SaveToken(token); + Interop.SaveToken(token.Value); return true; } @@ -119,9 +121,9 @@ private Request CreateRequest(Type type, string payload) public async Task QueryAsync(Type type, string payload) { - if (token == null) + if (!token.HasValue) { - token = await Interop.LoadTokenAsync(); + token.Value = await Interop.LoadTokenAsync(); EnsureAuthorization(); } diff --git a/src/Money.UI.Blazor/Services/TokenContainer.cs b/src/Money.UI.Blazor/Services/TokenContainer.cs new file mode 100644 index 00000000..7cb6ba43 --- /dev/null +++ b/src/Money.UI.Blazor/Services/TokenContainer.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Money.Services +{ + public class TokenContainer + { + public string Value { get; set; } + + public bool HasValue => !String.IsNullOrEmpty(Value); + } +} diff --git a/src/Money.UI.Blazor/Startup.cs b/src/Money.UI.Blazor/Startup.cs index 009a8d6e..bde21dfc 100644 --- a/src/Money.UI.Blazor/Startup.cs +++ b/src/Money.UI.Blazor/Startup.cs @@ -20,6 +20,7 @@ public void ConfigureServices(IServiceCollection services) services .AddTransient() .AddTransient() + .AddSingleton() .AddSingleton() .AddSingleton() .AddSingleton()