Skip to content

Commit

Permalink
#218 - Persistence for tokens, when persistent login is requested.
Browse files Browse the repository at this point in the history
  • Loading branch information
maraf committed Mar 5, 2019
1 parent a300837 commit c3ea040
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 8 deletions.
6 changes: 6 additions & 0 deletions src/Money.UI.Blazor/Interop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,11 @@ public static void StartSignalR(string url, string token)

public static void StopSignalR()
=> JSRuntime.Current.InvokeAsync<bool>("Money.StopSignalR");

public static void SaveToken(string token)
=> JSRuntime.Current.InvokeAsync<bool>("Money.SaveToken", token);

public static Task<string> LoadTokenAsync()
=> JSRuntime.Current.InvokeAsync<string>("Money.LoadToken");
}
}
9 changes: 8 additions & 1 deletion src/Money.UI.Blazor/Layouts/LoginInfo.cshtml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
@inject IQueryDispatcher Queries
@inject Navigator Navigator
@inject ApiClient ApiClient

<form action="/account/logout" method="post" id="logoutForm" class="navbar-right">
<form class="navbar-right" onsubmit="@OnLogoutAsync">
<ul class="nav navbar-nav navbar-right">
<li>
<a href="@Navigator.UrlUserManage()" onclick="@(() => Navigator.OpenUserManage())">
Expand All @@ -27,4 +28,10 @@
using (Loading.Start())
Profile = await Queries.QueryAsync(new GetProfile());
}

protected async Task OnLogoutAsync()
{
await ApiClient.LogoutAsync();
Navigator.OpenLogin();
}
}
27 changes: 21 additions & 6 deletions src/Money.UI.Blazor/Pages/Account/Login.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class LoginBase : BlazorComponent
protected string Password { get; set; }
protected bool IsPermanent { get; set; }

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

protected Task OnSubmitAsync()
=> LoginAsync(UserName, Password, IsPermanent);
Expand All @@ -30,11 +30,26 @@ protected Task OnDemoSubmitAsync()

private async Task LoginAsync(string userName, string password, bool isPermanent)
{
IsError = false;
if (!await ApiClient.LoginAsync(userName, password, isPermanent))
IsError = true;
else
Navigator.OpenSummary();
ErrorMessages.Clear();

if (Validate(userName, password))
{
if (!await ApiClient.LoginAsync(userName, password, isPermanent))
ErrorMessages.Add("User name and password don't match.");
else
Navigator.OpenSummary();
}
}

private bool Validate(string userName, string password)
{
if (String.IsNullOrEmpty(userName))
ErrorMessages.Add("Please, fill user name.");

if (String.IsNullOrEmpty(password))
ErrorMessages.Add("Please, fill password.");

return ErrorMessages.Count == 0;
}
}
}
16 changes: 16 additions & 0 deletions src/Money.UI.Blazor/Services/ApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ private void ClearAuthorization()
{
token = null;
http.DefaultRequestHeaders.Authorization = null;
Interop.SaveToken(null);
Interop.StopSignalR();
}

Expand Down Expand Up @@ -70,17 +71,32 @@ public async Task<bool> LoginAsync(string userName, string password, bool isPerm
{
token = response.Token;
EnsureAuthorization();

if (isPermanent)
Interop.SaveToken(token);

return true;
}

return false;
}

public async Task LogoutAsync()
{
ClearAuthorization();
}

private Request CreateRequest(Type type, string payload)
=> new Request() { Type = type.AssemblyQualifiedName, Payload = payload };

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

string url = queryMapper.FindUrlByType(type);
if (url != null)
{
Expand Down
17 changes: 16 additions & 1 deletion src/Money.UI.Blazor/wwwroot/js/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,20 @@ window.Money = {
return true;
},
StartSignalR: StartSignalR,
StopSignalR: StopSignalR
StopSignalR: StopSignalR,
SaveToken: function (token) {
if ("localStorage" in window) {
if (token == null)
window.localStorage.removeItem("token");
else
window.localStorage.setItem("token", token);
}
},
LoadToken: function () {
if ("localStorage" in window) {
return window.localStorage.getItem("token");
}

return null;
}
};

0 comments on commit c3ea040

Please sign in to comment.