Files
NTBug/web/Helpdesk.Web/Api/AuthApiClient.cs
T

26 lines
1.1 KiB
C#
Raw Normal View History

using System.Net;
using System.Net.Http.Json;
namespace Helpdesk.Web.Api;
/// <summary>
/// Anonymní endpointy <c>/api/v1/auth</c>. Záměrně bez <see cref="BearerTokenHandler"/>:
/// obnova tokenu běží uvnitř ověřování cookie a čtení tokenu z cookie by se zacyklilo.
/// </summary>
public sealed class AuthApiClient(HttpClient http)
{
public async Task<AuthResponse> LoginAsync(LoginRequest request, CancellationToken ct)
{
using var response = await http.PostAsJsonAsync("api/v1/auth/login", request, ct);
return await ApiResponse.ReadAsync<AuthResponse>(response, ct);
}
/// <summary>Obnoví pár tokenů. Neplatný nebo už použitý refresh token → <c>null</c>.</summary>
public async Task<TokenSet?> RefreshAsync(string refreshToken, CancellationToken ct)
{
using var response = await http.PostAsJsonAsync("api/v1/auth/refresh", new RefreshTokenRequest(refreshToken), ct);
if (response.StatusCode == HttpStatusCode.Unauthorized)
return null;
return (await ApiResponse.ReadAsync<AuthResponse>(response, ct)).Tokens;
}
}