54 lines
2.0 KiB
C#
54 lines
2.0 KiB
C#
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 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.");
|
|
}
|
|
}
|
|
} |