Skip to content

Commit

Permalink
#218 - Use authentication API from client.
Browse files Browse the repository at this point in the history
  • Loading branch information
maraf committed Mar 5, 2019
1 parent 9508d86 commit ebeb1cc
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 9 deletions.
File renamed without changes.
File renamed without changes.
9 changes: 3 additions & 6 deletions src/Money.UI.Blazor/Pages/Account/Login.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,10 @@ protected Task OnDemoSubmitAsync()
private async Task LoginAsync(string userName, string password, bool isPermanent)
{
IsError = false;

string token = await ApiClient.LoginAsync(userName, password, isPermanent);
if (string.IsNullOrEmpty(token))
{
if (!await ApiClient.LoginAsync(userName, password, isPermanent))
IsError = true;
return;
}
else
Navigator.OpenSummary();
}
}
}
39 changes: 36 additions & 3 deletions src/Money.UI.Blazor/Services/ApiClient.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Blazor;
using Money.Models.Api;
using Money.Users.Models;
using Neptuo;
using Neptuo.Exceptions.Handlers;
using System;
Expand All @@ -15,6 +16,8 @@ namespace Money.Services
{
public class ApiClient
{
private static string token;

private readonly HttpClient http;
private readonly CommandMapper commandMapper;
private readonly QueryMapper queryMapper;
Expand All @@ -31,12 +34,41 @@ public ApiClient(HttpClient http, CommandMapper commandMapper, QueryMapper query
this.queryMapper = queryMapper;
this.exceptionHandler = exceptionHandler;
http.BaseAddress = new Uri("http://localhost:63803");
//http.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoiZGVtbyIsImh0dHA6Ly9zY2hlbWFzLnhtbHNvYXAub3JnL3dzLzIwMDUvMDUvaWRlbnRpdHkvY2xhaW1zL25hbWVpZGVudGlmaWVyIjoiMjhmNGQxNzYtNjg5ZS00ZDRkLTlhMzgtYTg3MGQ5NzFhZDc5IiwiZXhwIjoxNTUyNzI2NDU2LCJpc3MiOiJodHRwczovL2xvY2FsaG9zdCIsImF1ZCI6Imh0dHBzOi8vbG9jYWxob3N0In0.4tSJlngLynld3Ul_HuicpO4zUERjYZ4FFjTrJxfE8Po");

EnsureAuthorization();
}

private void ClearAuthorization()
{
token = null;
http.DefaultRequestHeaders.Authorization = null;
}

private void EnsureAuthorization()
{
if (token != null)
http.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
}

public Task<string> LoginAsync(string userName, string password, bool isPermanent)
public async Task<bool> LoginAsync(string userName, string password, bool isPermanent)
{
throw new NotImplementedException();
LoginResponse response = await http.PostJsonAsync<LoginResponse>(
"/api/user/login",
new LoginRequest()
{
UserName = userName,
Password = password
}
);

if (!String.IsNullOrEmpty(response.Token))
{
token = response.Token;
EnsureAuthorization();
return true;
}

return false;
}

private Request CreateRequest(Type type, string payload)
Expand All @@ -50,6 +82,7 @@ public async Task<Response> QueryAsync(Type type, string payload)
HttpResponseMessage response = await http.PostAsync($"/api/query/{url}", new StringContent(payload, Encoding.UTF8, "text/json"));
if (response.StatusCode == HttpStatusCode.Unauthorized)
{
ClearAuthorization();
UnauthorizedAccessException exception = new UnauthorizedAccessException();
exceptionHandler.Handle(exception);
throw exception;
Expand Down

0 comments on commit ebeb1cc

Please sign in to comment.