diff --git a/src/Money.UI.Blazor/Pages/Account/Login.cshtml.cs b/src/Money.UI.Blazor/Pages/Account/Login.cshtml.cs index 4cdd93b3..a79cc450 100644 --- a/src/Money.UI.Blazor/Pages/Account/Login.cshtml.cs +++ b/src/Money.UI.Blazor/Pages/Account/Login.cshtml.cs @@ -1,4 +1,5 @@ using Microsoft.AspNetCore.Blazor.Components; +using Microsoft.AspNetCore.Blazor.Services; using Money.Services; using System; using System.Collections.Generic; @@ -16,12 +17,46 @@ public class LoginBase : BlazorComponent [Inject] internal ApiClient ApiClient { get; set; } + [Inject] + internal IUriHelper Uri { get; set; } + + [Parameter] + protected string ReturnUrl { get; set; } + protected string UserName { get; set; } protected string Password { get; set; } protected bool IsPermanent { get; set; } protected List ErrorMessages { get; } = new List(); + protected override void OnParametersSet() + { + base.OnParametersSet(); + SetReturnUrl(); + } + + private void SetReturnUrl() + { + string url = Uri.GetAbsoluteUri(); + int indexOfQuery = url.IndexOf('?'); + if (indexOfQuery >= 0) + { + string query = url.Substring(indexOfQuery + 1).ToLowerInvariant(); + string[] parameters = query.Split('&'); + foreach (string parameter in parameters) + { + string[] keyValue = parameter.Split('='); + if (keyValue[0] == "returnurl") + { + if (keyValue.Length == 2) + ReturnUrl = keyValue[1]; + + break; + } + } + } + } + protected Task OnSubmitAsync() => LoginAsync(UserName, Password, IsPermanent); @@ -36,6 +71,8 @@ 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 (ReturnUrl != null) + Uri.NavigateTo(ReturnUrl); else Navigator.OpenSummary(); } diff --git a/src/Money.UI.Blazor/Services/Navigator.cs b/src/Money.UI.Blazor/Services/Navigator.cs index 4d6459c4..0e49738d 100644 --- a/src/Money.UI.Blazor/Services/Navigator.cs +++ b/src/Money.UI.Blazor/Services/Navigator.cs @@ -67,7 +67,18 @@ public void OpenUserPassword() => uri.NavigateTo(UrlUserPassword()); public void OpenLogin() - => uri.NavigateTo(UrlAccountLogin()); + { + string loginUrl = UrlAccountLogin(); + string currentUrl = "/" + uri.ToBaseRelativePath(uri.GetBaseUri(), uri.GetAbsoluteUri()); + int indexOfReturnUrl = currentUrl.IndexOf("?returnUrl"); + if (indexOfReturnUrl >= 0) + currentUrl = currentUrl.Substring(0, indexOfReturnUrl); + + if (loginUrl != currentUrl) + { + uri.NavigateTo($"{loginUrl}?returnUrl={currentUrl}"); + } + } public void OpenRegister() => uri.NavigateTo(UrlAccountRegister());