mirror of
https://github.com/Shchoholiev/shopping-assistant-web-client.git
synced 2025-04-12 01:48:49 +00:00
SA-109 Moved auth logic to ApiClient from Middleware
This commit is contained in:
parent
8f5abc8f98
commit
f8a32070c5
@ -2,6 +2,7 @@
|
|||||||
using GraphQL;
|
using GraphQL;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using System.Net.Http.Headers;
|
using System.Net.Http.Headers;
|
||||||
|
using ShoppingAssistantWebClient.Web.Models.GlobalInstances;
|
||||||
|
|
||||||
namespace ShoppingAssistantWebClient.Web.Network;
|
namespace ShoppingAssistantWebClient.Web.Network;
|
||||||
|
|
||||||
@ -29,7 +30,9 @@ public class ApiClient
|
|||||||
{
|
{
|
||||||
await SetAuthenticationAsync();
|
await SetAuthenticationAsync();
|
||||||
|
|
||||||
return await _graphQLClient.SendQueryAsync<dynamic>(request);
|
var response = await _graphQLClient.SendQueryAsync<dynamic>(request);
|
||||||
|
|
||||||
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<T> QueryAsync<T>(GraphQLRequest request, string propertyName)
|
public async Task<T> QueryAsync<T>(GraphQLRequest request, string propertyName)
|
||||||
@ -116,7 +119,18 @@ public class ApiClient
|
|||||||
|
|
||||||
private async Task SetAuthenticationAsync()
|
private async Task SetAuthenticationAsync()
|
||||||
{
|
{
|
||||||
_graphQLClient.HttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", this.JwtToken);
|
var accessToken = await _authenticationService.GetAuthTokenAsync();
|
||||||
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", this.JwtToken);
|
if (!string.IsNullOrEmpty(accessToken))
|
||||||
|
{
|
||||||
|
this.JwtToken = accessToken;
|
||||||
|
|
||||||
|
GlobalUser.Id = _authenticationService.GetIdFromJwtToken(accessToken);
|
||||||
|
GlobalUser.Email = _authenticationService.GetEmailFromJwtToken(accessToken);
|
||||||
|
GlobalUser.Phone = _authenticationService.GetPhoneFromJwtToken(accessToken);
|
||||||
|
GlobalUser.Roles = _authenticationService.GetRolesFromJwtToken(accessToken);
|
||||||
|
|
||||||
|
_graphQLClient.HttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", this.JwtToken);
|
||||||
|
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", this.JwtToken);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -30,6 +30,7 @@ app.UseRouting();
|
|||||||
app.MapBlazorHub();
|
app.MapBlazorHub();
|
||||||
app.MapFallbackToPage("/_Host");
|
app.MapFallbackToPage("/_Host");
|
||||||
|
|
||||||
app.ConfigureGlobalUserMiddleware();
|
// Login moved to ApiClient
|
||||||
|
// app.ConfigureGlobalUserMiddleware();
|
||||||
|
|
||||||
app.Run();
|
app.Run();
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
@using Models.GlobalInstances
|
||||||
@inject NavigationManager Navigation
|
@inject NavigationManager Navigation
|
||||||
|
|
||||||
<div id="leftframe" class="left_frame">
|
<div id="leftframe" class="left_frame">
|
||||||
@ -51,7 +52,8 @@
|
|||||||
<div class="elements">
|
<div class="elements">
|
||||||
<div class="info_user">
|
<div class="info_user">
|
||||||
<img src="/images/avatar.jpg" alt="Avatar user">
|
<img src="/images/avatar.jpg" alt="Avatar user">
|
||||||
<span class="user_name">Semen Semenov</span>
|
<!-- Change to name -->
|
||||||
|
<span class="user_name">@GlobalUser.Id</span>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
Loading…
Reference in New Issue
Block a user