Added CORS, added specific type for token in AuthToken.cs, added login page to blazor client

This commit is contained in:
2024-08-30 15:10:25 +03:00
parent c631dc99d0
commit 802e42563e
15 changed files with 102 additions and 36 deletions

View File

@@ -413,7 +413,7 @@ namespace VisaApiClient
/// </summary>
/// <returns>Success</returns>
/// <exception cref="ApiException">A server side error occurred.</exception>
public virtual System.Threading.Tasks.Task<string> LoginAsync(string? email, string? password)
public virtual System.Threading.Tasks.Task<AuthToken> LoginAsync(string? email, string? password)
{
return LoginAsync(email, password, System.Threading.CancellationToken.None);
}
@@ -424,7 +424,7 @@ namespace VisaApiClient
/// </summary>
/// <returns>Success</returns>
/// <exception cref="ApiException">A server side error occurred.</exception>
public virtual async System.Threading.Tasks.Task<string> LoginAsync(string? email, string? password, System.Threading.CancellationToken cancellationToken)
public virtual async System.Threading.Tasks.Task<AuthToken> LoginAsync(string? email, string? password, System.Threading.CancellationToken cancellationToken)
{
var client_ = _httpClient;
var disposeClient_ = false;
@@ -475,7 +475,7 @@ namespace VisaApiClient
var status_ = (int)response_.StatusCode;
if (status_ == 200)
{
var objectResponse_ = await ReadObjectResponseAsync<string>(response_, headers_, cancellationToken).ConfigureAwait(false);
var objectResponse_ = await ReadObjectResponseAsync<AuthToken>(response_, headers_, cancellationToken).ConfigureAwait(false);
if (objectResponse_.Object == null)
{
throw new ApiException("Response was null which was not expected.", status_, objectResponse_.Text, headers_, null);
@@ -1583,6 +1583,14 @@ namespace VisaApiClient
}
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.1.0.0 (NJsonSchema v11.0.2.0 (Newtonsoft.Json v13.0.0.0))")]
public partial class AuthToken
{
[Newtonsoft.Json.JsonProperty("token", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public string? Token { get; set; } = default!;
}
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.1.0.0 (NJsonSchema v11.0.2.0 (Newtonsoft.Json v13.0.0.0))")]
public enum AuthorityRequestStatuses
{

View File

@@ -4,11 +4,9 @@ namespace VisaApiClient
{
public class ClientBase
{
private const string LoginPath = "users/login";
protected AuthToken? AuthToken { get; private set; }
protected string? AuthToken { get; private set; }
protected void SetAuthToken(string token)
public void SetAuthToken(AuthToken token)
{
AuthToken = token;
}
@@ -16,7 +14,9 @@ namespace VisaApiClient
protected Task<HttpRequestMessage> CreateHttpRequestMessageAsync(CancellationToken cancellationToken)
{
var msg = new HttpRequestMessage();
msg.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", AuthToken);
msg.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", AuthToken?.Token);
return Task.FromResult(msg);
}
@@ -25,15 +25,11 @@ namespace VisaApiClient
=> await Task.CompletedTask;
protected async Task PrepareRequestAsync(HttpClient client, HttpRequestMessage request, StringBuilder urlBuilder, CancellationToken cancellationToken)
=> await Task.CompletedTask;
{
await Task.CompletedTask;
}
protected async Task ProcessResponseAsync(HttpClient client, HttpResponseMessage response, CancellationToken cancellationToken)
{
if (response.RequestMessage!.RequestUri!.AbsolutePath == LoginPath)
{
var token = await response.Content.ReadAsStringAsync(cancellationToken);
SetAuthToken(token);
}
}
=> await Task.CompletedTask;
}
}