using System.Net;
using System.Net.Http.Json;
namespace Helpdesk.Web.Api;
///
/// Anonymní endpointy auth/*. Záměrně bez :
/// obnova tokenu běží uvnitř ověřování cookie a čtení tokenu z cookie by se zacyklilo.
///
public sealed class AuthApiClient(HttpClient http)
{
public async Task LoginAsync(LoginRequest request, CancellationToken ct)
{
using var response = await http.PostAsJsonAsync("auth/login", request, ct);
return await ApiResponse.ReadAsync(response, ct);
}
/// Registrace. Roli (student / vyučující) určí API podle domény e-mailu.
public async Task RegisterAsync(RegisterRequest request, CancellationToken ct)
{
using var response = await http.PostAsJsonAsync("auth/register", request, ct);
return await ApiResponse.ReadAsync(response, ct);
}
public async Task ConfirmEmailAsync(string token, CancellationToken ct)
{
using var response = await http.PostAsJsonAsync("auth/confirm-email", new EmailTokenRequest(token), ct);
await ApiResponse.EnsureSuccessAsync(response, ct);
}
/// API odpoví stejně, ať účet existuje, nebo ne – stránka tak neprozradí registrované e-maily.
public async Task ForgotPasswordAsync(string email, CancellationToken ct)
{
using var response = await http.PostAsJsonAsync("auth/forgot-password", new ForgotPasswordRequest(email), ct);
await ApiResponse.EnsureSuccessAsync(response, ct);
}
public async Task ResetPasswordAsync(ResetPasswordRequest request, CancellationToken ct)
{
using var response = await http.PostAsJsonAsync("auth/reset-password", request, ct);
await ApiResponse.EnsureSuccessAsync(response, ct);
}
/// Obnoví pár tokenů. Neplatný nebo už použitý refresh token → null.
public async Task RefreshAsync(string refreshToken, CancellationToken ct)
{
using var response = await http.PostAsJsonAsync("auth/refresh", new RefreshTokenRequest(refreshToken), ct);
if (response.StatusCode == HttpStatusCode.Unauthorized)
return null;
return (await ApiResponse.ReadAsync(response, ct)).Tokens;
}
}