Skip to content

Commit

Permalink
#218 - Using query string parameter returnUrl for navigation after su…
Browse files Browse the repository at this point in the history
…ccessful login.
  • Loading branch information
maraf committed Mar 6, 2019
1 parent 37f8dac commit b399d7a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
37 changes: 37 additions & 0 deletions src/Money.UI.Blazor/Pages/Account/Login.cshtml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Blazor.Components;
using Microsoft.AspNetCore.Blazor.Services;
using Money.Services;
using System;
using System.Collections.Generic;
Expand All @@ -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<string> ErrorMessages { get; } = new List<string>();

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);

Expand All @@ -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();
}
Expand Down
13 changes: 12 additions & 1 deletion src/Money.UI.Blazor/Services/Navigator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit b399d7a

Please sign in to comment.