First successfull build
This commit is contained in:
@@ -1,6 +1,54 @@
|
||||
namespace EnvelopeGenerator.ReceiverUI.Client.Services
|
||||
using EnvelopeGenerator.ReceiverUI.Client.Services.Base;
|
||||
|
||||
namespace EnvelopeGenerator.ReceiverUI.Client.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Spricht mit dem bestehenden AuthController der API.
|
||||
/// Die API erkennt den Nutzer über das Cookie "AuthToken" automatisch.
|
||||
/// </summary>
|
||||
public class AuthService : ApiServiceBase, IAuthService
|
||||
{
|
||||
public class AuthService
|
||||
public AuthService(HttpClient http, ILogger<AuthService> logger) : base(http, logger) { }
|
||||
|
||||
public async Task<ApiResponse> CheckAuthAsync(string? role = null, CancellationToken ct = default)
|
||||
{
|
||||
var endpoint = role is not null ? $"api/auth/check?role={role}" : "api/auth/check";
|
||||
try
|
||||
{
|
||||
var response = await Http.GetAsync(endpoint, ct);
|
||||
return response.IsSuccessStatusCode
|
||||
? ApiResponse.Success((int)response.StatusCode)
|
||||
: ApiResponse.Failure((int)response.StatusCode);
|
||||
}
|
||||
catch (HttpRequestException ex)
|
||||
{
|
||||
Logger.LogError(ex, "HTTP error calling GET {Endpoint}", endpoint);
|
||||
return ApiResponse.Failure(0, "Verbindung zum Server fehlgeschlagen.");
|
||||
}
|
||||
catch (TaskCanceledException)
|
||||
{
|
||||
return ApiResponse.Failure(0, "Anfrage abgebrochen.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ApiResponse> LogoutAsync(CancellationToken ct = default)
|
||||
{
|
||||
const string endpoint = "api/auth/logout";
|
||||
try
|
||||
{
|
||||
var response = await Http.PostAsync(endpoint, null, ct);
|
||||
return response.IsSuccessStatusCode
|
||||
? ApiResponse.Success((int)response.StatusCode)
|
||||
: ApiResponse.Failure((int)response.StatusCode);
|
||||
}
|
||||
catch (HttpRequestException ex)
|
||||
{
|
||||
Logger.LogError(ex, "HTTP error calling POST {Endpoint}", endpoint);
|
||||
return ApiResponse.Failure(0, "Verbindung zum Server fehlgeschlagen.");
|
||||
}
|
||||
catch (TaskCanceledException)
|
||||
{
|
||||
return ApiResponse.Failure(0, "Anfrage abgebrochen.");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user