2026-12-02 00:26:42 +01:00
|
|
|
|
using System.Net;
|
|
|
|
|
|
using System.Net.Http.Json;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Helpdesk.Web.Api;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2026-12-02 18:54:15 +01:00
|
|
|
|
/// Anonymní endpointy <c>auth/*</c>. Záměrně bez <see cref="BearerTokenHandler"/>:
|
2026-12-02 00:26:42 +01:00
|
|
|
|
/// 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)
|
|
|
|
|
|
{
|
2026-12-02 18:54:15 +01:00
|
|
|
|
using var response = await http.PostAsJsonAsync("auth/login", request, ct);
|
2026-12-02 00:26:42 +01:00
|
|
|
|
return await ApiResponse.ReadAsync<AuthResponse>(response, ct);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-12-02 00:31:07 +01:00
|
|
|
|
/// <summary>Registrace. Roli (student / vyučující) určí API podle domény e-mailu.</summary>
|
|
|
|
|
|
public async Task<UserDto> RegisterAsync(RegisterRequest request, CancellationToken ct)
|
|
|
|
|
|
{
|
2026-12-02 18:54:15 +01:00
|
|
|
|
using var response = await http.PostAsJsonAsync("auth/register", request, ct);
|
2026-12-02 00:31:07 +01:00
|
|
|
|
return await ApiResponse.ReadAsync<UserDto>(response, ct);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-12-04 20:14:00 +01:00
|
|
|
|
public async Task ConfirmEmailAsync(ConfirmEmailRequest request, CancellationToken ct)
|
2026-12-02 00:31:07 +01:00
|
|
|
|
{
|
2026-12-04 20:14:00 +01:00
|
|
|
|
using var response = await http.PostAsJsonAsync("auth/confirm-email", request, ct);
|
2026-12-02 00:31:07 +01:00
|
|
|
|
await ApiResponse.EnsureSuccessAsync(response, ct);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>API odpoví stejně, ať účet existuje, nebo ne – stránka tak neprozradí registrované e-maily.</summary>
|
|
|
|
|
|
public async Task ForgotPasswordAsync(string email, CancellationToken ct)
|
|
|
|
|
|
{
|
2026-12-02 18:54:15 +01:00
|
|
|
|
using var response = await http.PostAsJsonAsync("auth/forgot-password", new ForgotPasswordRequest(email), ct);
|
2026-12-02 00:31:07 +01:00
|
|
|
|
await ApiResponse.EnsureSuccessAsync(response, ct);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task ResetPasswordAsync(ResetPasswordRequest request, CancellationToken ct)
|
|
|
|
|
|
{
|
2026-12-02 18:54:15 +01:00
|
|
|
|
using var response = await http.PostAsJsonAsync("auth/reset-password", request, ct);
|
2026-12-02 00:31:07 +01:00
|
|
|
|
await ApiResponse.EnsureSuccessAsync(response, ct);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-12-02 00:26:42 +01:00
|
|
|
|
/// <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)
|
|
|
|
|
|
{
|
2026-12-02 18:54:15 +01:00
|
|
|
|
using var response = await http.PostAsJsonAsync("auth/refresh", new RefreshTokenRequest(refreshToken), ct);
|
2026-12-02 00:26:42 +01:00
|
|
|
|
if (response.StatusCode == HttpStatusCode.Unauthorized)
|
|
|
|
|
|
return null;
|
|
|
|
|
|
return (await ApiResponse.ReadAsync<AuthResponse>(response, ct)).Tokens;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|