From ebeb1cc817ea8761b7187cae6ab07318faf63402 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Fi=C5=A1era?= Date: Tue, 5 Mar 2019 18:30:47 +0100 Subject: [PATCH] #218 - Use authentication API from client. --- .../Models/User}/LoginRequest.cs | 0 .../Models/User}/LoginResponse.cs | 0 .../Pages/Account/Login.cshtml.cs | 9 ++--- src/Money.UI.Blazor/Services/ApiClient.cs | 39 +++++++++++++++++-- 4 files changed, 39 insertions(+), 9 deletions(-) rename src/{Money.Api/Users/Models => Money.UI.Blazor/Models/User}/LoginRequest.cs (100%) rename src/{Money.Api/Users/Models => Money.UI.Blazor/Models/User}/LoginResponse.cs (100%) diff --git a/src/Money.Api/Users/Models/LoginRequest.cs b/src/Money.UI.Blazor/Models/User/LoginRequest.cs similarity index 100% rename from src/Money.Api/Users/Models/LoginRequest.cs rename to src/Money.UI.Blazor/Models/User/LoginRequest.cs diff --git a/src/Money.Api/Users/Models/LoginResponse.cs b/src/Money.UI.Blazor/Models/User/LoginResponse.cs similarity index 100% rename from src/Money.Api/Users/Models/LoginResponse.cs rename to src/Money.UI.Blazor/Models/User/LoginResponse.cs diff --git a/src/Money.UI.Blazor/Pages/Account/Login.cshtml.cs b/src/Money.UI.Blazor/Pages/Account/Login.cshtml.cs index 1a78d54d..a299bfa0 100644 --- a/src/Money.UI.Blazor/Pages/Account/Login.cshtml.cs +++ b/src/Money.UI.Blazor/Pages/Account/Login.cshtml.cs @@ -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(); } } } diff --git a/src/Money.UI.Blazor/Services/ApiClient.cs b/src/Money.UI.Blazor/Services/ApiClient.cs index 0f419229..91838dfe 100644 --- a/src/Money.UI.Blazor/Services/ApiClient.cs +++ b/src/Money.UI.Blazor/Services/ApiClient.cs @@ -1,5 +1,6 @@ using Microsoft.AspNetCore.Blazor; using Money.Models.Api; +using Money.Users.Models; using Neptuo; using Neptuo.Exceptions.Handlers; using System; @@ -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; @@ -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 LoginAsync(string userName, string password, bool isPermanent) + public async Task LoginAsync(string userName, string password, bool isPermanent) { - throw new NotImplementedException(); + LoginResponse response = await http.PostJsonAsync( + "/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) @@ -50,6 +82,7 @@ public async Task 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;