Skip to content

Commit

Permalink
#218 - Abstraction of token store and redirecting from login and regi…
Browse files Browse the repository at this point in the history
…ster when authenticated.
  • Loading branch information
maraf committed Mar 12, 2019
1 parent c84f36a commit ec5d143
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 15 deletions.
21 changes: 18 additions & 3 deletions src/Money.UI.Blazor/Pages/Account/Login.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

Expand All @@ -29,6 +32,12 @@ public class LoginBase : BlazorComponent

protected List<string> ErrorMessages { get; } = new List<string>();

protected override void OnInit()
{
if (Token.HasValue)
NavigateAway();
}

protected override void OnParametersSet()
{
base.OnParametersSet();
Expand All @@ -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))
Expand Down
11 changes: 10 additions & 1 deletion src/Money.UI.Blazor/Pages/Account/Register.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> ErrorMessages { get; } = new List<string>();
protected List<string> ErrorMessages { get; } = new List<string>();

protected override void OnInit()
{
if (Token.HasValue)
Navigator.OpenSummary();
}

protected async Task OnSubmitAsync()
{
Expand Down
24 changes: 13 additions & 11 deletions src/Money.UI.Blazor/Services/ApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,24 @@ 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;
private readonly IExceptionHandler exceptionHandler;

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;
Expand All @@ -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();
Expand All @@ -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());
}
Expand All @@ -82,11 +84,11 @@ public async Task<bool> 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;
}
Expand Down Expand Up @@ -119,9 +121,9 @@ private Request CreateRequest(Type type, string payload)

public async Task<Response> QueryAsync(Type type, string payload)
{
if (token == null)
if (!token.HasValue)
{
token = await Interop.LoadTokenAsync();
token.Value = await Interop.LoadTokenAsync();
EnsureAuthorization();
}

Expand Down
15 changes: 15 additions & 0 deletions src/Money.UI.Blazor/Services/TokenContainer.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
1 change: 1 addition & 0 deletions src/Money.UI.Blazor/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public void ConfigureServices(IServiceCollection services)
services
.AddTransient<Navigator>()
.AddTransient<ApiClient>()
.AddSingleton<TokenContainer>()
.AddSingleton<QueryString>()
.AddSingleton<CommandMapper>()
.AddSingleton<QueryMapper>()
Expand Down

0 comments on commit ec5d143

Please sign in to comment.